Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JSP Passing Arraylist

Posted on 2002-04-24
14
1,199 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
map related example 6 55
type mismatch (Object[] to double[] 4 33
JavaFX TableView not displaying correctly 3 54
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…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

792 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