[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

JSP Passing Arraylist

Posted on 2002-04-24
14
Medium Priority
?
1,214 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Accepted Solution

by:
cheekycj earned 200 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: 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.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

650 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