File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes passing an ArrayList from a servlet to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing an ArrayList from a servlet to JSP" Watch "passing an ArrayList from a servlet to JSP" New topic
Author

passing an ArrayList from a servlet to JSP

Maggie Taylor
Greenhorn

Joined: Feb 14, 2006
Posts: 10
Hi !

I am trying to call a JSP page from a servlet.

I first collect the data in the form of Lists in the servlet. Then I need to pass information like a list of names, address, course, etc. (NameList, AddressList, CourseList) to the JSP page which then displays it in the form of a table.

I'm aware of passing a single value using session from a servlet to JSP. How do I go about passing ArrayLists from a servlet to JSP?

Is it possible to declare class variables in the servlet -
static List NameList;
static List AddressList;
Add data collected into these lists, and then access these lists from the JSP page. Is this a good way to go about?

-M
[ February 20, 2006: Message edited by: Maggie Taylor ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

No, class variables will only create thread safety issues that will cause you no end of problems.

What do you mean by "calling" a JSP from a servlet? Are you using a dispatcher to forward to the JSP, or are you redirecting via the response?

Most commonly, data to be passed to the JSP is place in request scope as scoped variables (via the setAttribute() method), and a request dispatcher is employed to forward control to the JSP.
[ February 20, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
Originally posted by Maggie Taylor:
Hi !

I'm aware of passing a single value using session from a servlet to JSP. How do I go about passing ArrayLists from a servlet to JSP?



what do you mean by passing a single value from session from servlet ?

Store your list in a helperclass like UserData.java which has getter and setter for NameList, AddressList, CourseList Arraylist .. Put this helper class in RequestScope and retreive it from JSP. Ranchers, correct it if this is a bad idea !

By the way, its not a right approach to declare class level variables in servlets as they are not thread safe..

Thanks,
Rajeev.
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
I think Bear and I were replying this post at the same time
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Hi Rajeev,
What do you mean by following,



By the way, its not a right approach to declare class level variables in servlets as they are not thread safe..



I think that using wrapper class for encapsulating data (e.g. UserData.java) for setting values in its object and then sending that object or Collection of objects, across components is a stanadard practice.
Isn't it a design pattern called Data Object?
Please clarify.

Abhishek
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

I think that using wrapper class for encapsulating data (e.g. UserData.java) for setting values in its object and then sending that object or Collection of objects, across components is a stanadard practice.


We use wrapper class to send custom request and response objects. Standard practice of sending objects between web components is using setAttribute(String, Object) method in one of the available three scopes(Application,Session,Request).


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Abhishek Asthana:
Isn't it a design pattern called Data Object?
Please clarify.


Its known as Value Object or Transfer Object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing an ArrayList from a servlet to JSP
 
Similar Threads
autoentry of fields on an html page
html form that contains a list of options drawn from db
Undefined attribute name Items compilation error using jstl
how to combine between two arraylist if has same value
pass data from JSP-JSP-SERVLET-DATABASE