Solved

JSP Passing Arraylist

Posted on 2002-04-24
14
1,190 Views
Last Modified: 2012-05-04
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

0
Comment
Question by:barnarp
  • 5
  • 3
  • 3
  • +3
14 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 6965243
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
 
LVL 2

Expert Comment

by:flumpman
ID: 6965245
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
 

Author Comment

by:barnarp
ID: 6965273
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
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 6965549
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6965717
you could also store it in the servletContext

ie:

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

then in your other page:

object = (objecttype) getServletContext().getAttribute("varname");
0
 

Expert Comment

by:jodear
ID: 6967638
>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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6968603
jodear: why propose an answer especially if it's basically the same as what cheekycj's comment is?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:cheekycj
ID: 6968604
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6968612
same sentiment at the same exact time <grinning>
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6968620
lol
0
 

Author Comment

by:barnarp
ID: 6968629
yeah

weird!
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6968638
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
 

Author Comment

by:barnarp
ID: 6968656
thanks , it helped alot.

was busy clearing jodears answer and accepting yours.

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

Regards
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6968676
Glad I could help and Thanx for the "A".

CJ
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 50
eclipse formatting 6 73
Java / Linux and Regular Expressions 11 71
simple java question 3 44
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now