JSP Passing Arraylist

hi,

Is it possible to pass an ArrayList from one page to another?

If so, how do I "receive" it on the second page?

This code doesn't work:

ArrayList QueryRows = new ArrayList();
QueryRows = request.getParameter(QueryRows);

Any help please

barnarpAsked:
Who is Participating?
 
cheekycjConnect With a Mentor Commented:
you can attach to the request using
request.setAttribute("varname", object);
then forward to the second jsp/servlet
and then use
object = (objecttype) request.getAttribute("varname");

CJ
0
 
rjackmanCommented:
put it into the Session object
use following for inserting ArrayList in Session
ArrayList QueryRows = new ArrayList();

session.setAttribute("array",QueryRows);

on the next page use the following code to retrieve the array list

ArrayList arrList = (ArrayList)session.getAttribute("array");

cheers
RJ
0
 
flumpmanCommented:
What is it that you are trying to achieve?

the Request.getParameter(String) method returns a String so you can't assign it to an ArrayList.

You could put the ArrayList into the user's Session in the first page and then access it from the second page.

e.g. in first page
HttpSession session = request.getSession(true);
ArrayList myArrayList = new ArrayList();
...
session.setAttribute("myArrayList", myArrayList);

in second page:
HttpSession session = request.getSession(false);
ArrayList myArrayList = (ArrayList) session.getAttribute("myArrayList");
...

Note: if you are using a servlet API < 2.2 you will have to use getValue/putValue instead of getAttribute/setAttribute.
Another method would be to write the values (that you need) from the ArrayList into the URL and then read it back in the second page, it depends on the context of the pages.

--
flumpman
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
barnarpAuthor Commented:
thanks,

the session was my second option, but just wondered if an array could be passed from one page to another like a string.

I already store a resultset from a database query in the session, but it the user do a search ,a different recordset will be displayed. i just wanted to pass the "Search recordset" as an araay to the display page instead of removing the existing array from the session and adding the new array.

I guess I can also just add another recordset array to the session.

Dont know how resource intensive that would be though.

Later
0
 
bobbit31Commented:
you could also store it in the servletContext

ie:

getServletContext().setAttribute("varname", object);

then in your other page:

object = (objecttype) getServletContext().getAttribute("varname");
0
 
jodearCommented:
>the session was my second option, but just wondered if an
>array could be passed from one page to another
>like a string.

This can be done, but its better if you follow cheekycj's suggestion above.  His suggestion passed your ArrayList as is to the next page thus you just need to retrieve it as is.  His example is:

1st JSP to send array through HttpServletRequest object (and not through the Session) contains:
...
<%
request.setAttribute("arrayvar", QueryRows);
%>

2nd JSP to retrieve the array from the HttpServletRequest object (not thru the Session) contains:
...
<%
ArrayList QueryRows = (ArrayList)request.getAttribute("arrayvar");
%>

The request object though is only accessible to the page you called (the 2nd JSP) unlike the session object which is accessible throughout the user's session as long as you are calling JSPs with <%@ page session="true"> set.

0
 
bobbit31Commented:
jodear: why propose an answer especially if it's basically the same as what cheekycj's comment is?
0
 
cheekycjCommented:
jodear: Why did you propose an answer and just agree with my comment?

Please refer to the bottom of this page for comments vs answers policies.

CJ
0
 
cheekycjCommented:
same sentiment at the same exact time <grinning>
0
 
bobbit31Commented:
lol
0
 
barnarpAuthor Commented:
yeah

weird!
0
 
cheekycjCommented:
barnap: Did any of the comments help?  Were you able to get one of the alternative approaches to work or do you need more assistance from us?

CJ
0
 
barnarpAuthor Commented:
thanks , it helped alot.

was busy clearing jodears answer and accepting yours.

net very slow this side of world today (South Africa)

Regards
0
 
cheekycjCommented:
Glad I could help and Thanx for the "A".

CJ
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.