aspose file tools*
The moose likes Servlets and the fly likes Session Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Object" Watch "Session Object" New topic
Author

Session Object

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi All,

Please find the piece of code below:


and the Result.jsp is as follows :


The output from log is as follows


signifying that Session object is not created (since false in getSession())

but the Result.jsp is displayed as


Now my question is when Null is displayed in the log, where did SessionID come from and is getting displayed in Result.jsp. I mean session is not created as false is passed as a parameter for getSession() method.

I'm bit confused here with these sessions topic. Please some one show me the light


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
In a JSP, a session object is automatically defined and made available.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Jesus Angeles:
In a JSP, a session object is automatically defined and made available.


... if you make a reference to the implicit session object, which you do.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You could do this in the JSP too, which uses the session but NOT the implicit session object:

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Actually I could not understand the replies given for my question. Does this mean that the session object referred in Servlet is different from what being used in jsp?

One more update on my question.

When I start the server for the first time and run my servlet, I'm getting Null for the session object, but in Result.jsp session id and object is displayed as mentioned initially.

But when I run the servlet again without restarting the server i.e refreshing the page, this time Null is not displayed. This time object is displayed in both servelt and jsp.

Can anyone explain me the phenomenon here?
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Maybe our statement above was not complete.

It is like this.

First of all, you always have 1 or 0 session object alive. The session object in jsp and servlet are the same instance.

In a servlet, you have to create your own session using the getSession(true). If one exist already, the getSession(true) returns the existing session (a session object which could have been created either in the servlet or jsp).

In the jsp, if there is no session object yet, it is created automatically for you.

Does this mean that the session object referred in Servlet is different from what being used in jsp?


There can only be 1 session object at most at one time. It is shared by all servlets and jsps during that entire conversation with the server. Of course, you can kill it (using invalidate method) then create a new one, but there is always 1 at a time.


But when I run the servlet again without restarting the server i.e refreshing the page, this time Null is not displayed. This time object is displayed in both servelt and jsp.

Can anyone explain me the phenomenon here?

Your conversation (session) with the server is still alive. Your session object created in the jsp previously is still there. The server is considering your old conversation and this new one, as from the same user or session. Try close the browser and open a new one.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Maybe I should have written it this way:

In a JSP, a session object is automatically defined and made available. If there is already an existing session object (probably created somewhere else like another jsp or servlet), that existing session object is returned.
 
Don't get me started about those stupid light bulbs.
 
subject: Session Object