开发者

Passing list/array between JSP pages

开发者 https://www.devze.com 2023-04-10 09:02 出处:网络
I\'m trying to pass a List between two JSP pages that I have. This is a list of objects that is of a class that I wrote.

I'm trying to pass a List between two JSP pages that I have. This is a list of objects that is of a class that I wrote.

How do I pass this list between JS开发者_运维知识库P pages? request.setAttribute seems to work for strings, but not anything else. And, if this cannot be easily done with a list, I can convert the list to an array and pass it that way, no problem.


The first thing is that a very bad design will lead to such questions as passing lists between different JSP pages. The "nip the evil at the bud" will be to create a separate java class which contains the list and initializes it, then you can access the list at as many jsp pages as you want.

But incase you really want to do, you can put the list in the session.

request.getSession().setAttribute("list",myListObject);

Then on the other page you can get

List<MyType>myListObject=(List<MyType>) request.getSession().getAttribute("list");

And you should clear the list from the session after you do not require it,

request.getSession().removeAttribute("list");


The simplest answer is: it depends.

If you have e.g. one.jsp and you call redirect to second.jsp - you can use request scope

<c:set var="list" value="${yourListObject}" scope="request" />

If you have one.jsp and few pages later you want to display your list, then you should use session scope:

<c:set var="list" value="${yourListObject}" scope="session" />

to display your list on second.jsp:

${list}

yourListObject you can replace by

  • <%= Java expression %>
  • use bean which has this list and just pass the reference here
0

精彩评论

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