• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing an ArrayList from a servlet to JSP

 
Maggie Taylor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Bear and I were replying this post at the same time
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhishek Asthana:
Isn't it a design pattern called Data Object?
Please clarify.


Its known as Value Object or Transfer Object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic