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.
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.
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.
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.
posted 13 years ago
Thanks for your clarifications on using terminology.. I stand correct it..
posted 13 years ago
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.