• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sending session object from servlt-multiple jsp's

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

Can anyone clarify me whether I can send a session object created in servlet to multiple JSP's as I have the same set of values required in various JSP's..

Initially I wrote entire code in a JSP page and forwarded the result to the next JSP page, but as the same set of data is required in multiple JSP's I am thinking of writing it in a servlet. Can I achieve my task like that.

Any sample code appreciated..


Thanks in advance
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sree M,
Welcome to JavaRanch!

We're pleased to have you here with us in the Servlets forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
The first name can be an initial but not the last.

You can change it here
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As to your question:
You don't send a session object anywhere.
You simply bind objects to it.
All of the servlets and JSPs, involved in a user's session, have access to the session object. It doesn't matter whether you use a servlet or a JSP to bind the objects to session. The results will be the same.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sree,

The best practice is to put all business logic in servlets (or better yet, classes called by servlets) and not in JSPs. JSPs should be strictly presentation.

If you have data that needs to be shared by multiple JSPs in the same request, you should store the data in the Request object as an attribute.

If you have data that needs to be saved across multiple http requests, store the data as an attribute in the Session object. Yes, the Session object is accessible by all JSPs and servlets that are executing for the given user/session.
 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben

Thanks for your clarifications on using terminology.. I stand correct it..

 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott

Really pleased to see your comments on my query. Now I am a bit clear about using "session" object in JSP/Servlet's. Actually I am very poor at servlet's so I worked with JSP's most of the time. Recently I started off working using Bean classes where I am doing all backend data processing, where I actually faced the problem of sharing the same backend information among multiple JSP's. Here I am writing individual JSP's (I will call them intermediate files to store data from backend in arraylist) for every HTTP request and binding the corresponding values to a session object which I am again forwarding to my Main JSP Form using <jsp:forward> tag. I need some of those values again in my 2nd form where I felt need of writing the same individual JSP's and I am forwarding to my 2nd form again using <jsp:forward> tag.

After seeing the redundancy in files I felt that Servlet's would suit my need to avoid duplication.

Can you refer me any good site where I can have some valuable information about Servlet's along with some examples to better understand the concepts of Servlets.


Again thanking you for giving me such a valuable information.

 
Sheriff
Posts: 67746
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
Perhaps this article in the JavaRanch Journal might help with understanding the patterns used in web applications.
 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give me any link where I can learn Servlets and jsp's effectively
reply
    Bookmark Topic Watch Topic
  • New Topic