File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Session Object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Object creation" Watch "Session Object creation" New topic

Session Object creation

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
i doubt when a session is created

i read the below line will create the new session
HttpSession.getSession(true);---> return the new session object if none is associated with the request.

how this object can be made asssociate to a particular user like session 1 is for user 1 and session 2 for user 2 and so on....

and if the session become invalid (after some expire time) how the user is abanded from proceeds further saying your session is expires...

how the above basic functionality is acheived..

Any one please....
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121

Association of a Session object to a user is done by the container. Apart from calling getSession() method, a servlet programmer need not do any extra things.

This association is done by using a cookie called jsessionid that is sent by the container everytime in the response. The browser sends this back with every subsequent request, so the container can identify the session object for this user. If cookies are disabled in the client, then you have to use encodeURL()/encodeRedirectURL() methods to acheive this.

You can get this id using getSessionId() method in the HttpSession object.

NOTE: getSession(false) returns a HttpSession object only if a session already exists for this user. Else, it returns null.

keep smilin :: sravan<br /><a href="" target="_blank" rel="nofollow">I scribble here</a>
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Get the session using getSession(false). If gets null then you can handle as you want.

Or store an attribute in the session say.. .

Boolean isValid

Get the session using getSession(). Check the attribute isValid if gets null then forward the request to sessionExpired.jsp
I agree. Here's the link:
subject: Session Object creation
It's not a secret anymore!