开发者

In JSP, how to pass ArrayList using anchor tag?

I am a total newbie in JSP. All I can do is receive data from form tags from another web page. I don't know any MCV(MVC?) nor custom tag, session, cookies or anything deeper than that.

All I need is to pass an ArrayList to another dynamically generated JSP page using an anchor tag. Here is the specification.

Basically I have a 2-dimensional ArrayList. The first dimension is iterated and listed in the first JSP page.

So the first JSP page would look like this:


This is ArrayList1. The size is 23.

This is ArrayList2. The size is 19.

This is ArrayList3. The size is 12.

..

..

..

This is ArrayList(n). The size is 1.


Each line is an anchor tag and when a user clicks it, the corresponding ArrayList is passed to the second JSP page.

The second JSP page, all it does is receive an ArrayList and just iterate through it and list the data in it. So there needs to be only one "second JSP page" because it will receive only one ArrayList at a time. It just needs to be dynamic.

I know how to iterate through an ArrayList in a JSP page. What I don't k开发者_JAVA技巧now is how to pass ArrayLists to another JSP page using an anchor tag.

Any tips or methods are welcome. I did some searching of my own, but all the comments are just too high-level for me. I'd appreciate any help. Thanks.


I think you should not do this way. Instead, you can do the following way. It is easier and more possible.

Idea: - set all arraylist to

request.setAttribute("arraylist1",Arraylist1);
request.setAttribute("arraylist2",Arraylist2);
.....

- In each anchor tag, you place an id to recognize which link is click

(e.g. <a href="b.jsp?id=arraylist1">your link </a>)  

- In the b.jsp, get id parameter which this line of code

String selectedLink = request.getParameter("id")
  • Compare if selectedLink = arraylist1
  • List selectedArray = (List)request.getAttribute(selectedLink);

Hope this useful

NathanPhan


  1. you might dont want to this.
  2. if you really want to do this, then
  3. it is not really JSP related question, instead it is HTTP related question
  4. you can join the content in the array with comma and append the whole string as a query string component in the url.

example:

in a.jsp you have a link:

b.jsp?array=1,2,3,4,5

in b.jsp you interpret the array string and get an arraylist of:

[1,2,3,4,5]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜