Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

passing an ArrayList from a servlet to JSP

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67641
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Bear and I were replying this post at the same time
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic