Solved

JSP Passing Arraylist

Posted on 2002-04-24
14
1,188 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

758 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

21 Experts available now in Live!

Get 1:1 Help Now