Concept Of Session In J2EE Session Tracking In J2EE Session Management Seesion Concept Servlet Basics Servlet Guide Session Tracking

Sessions In J2EE(How to maintain session in java web applications)

Session in J2EE application is the time of interaction between the client and the server.A session in used to retain the state of a client.As we know that http is connectionless protocol,after each request and response cycle between the client and the server,the connection is broken. It seems logical to break the connection as bandwidth is an expensive resource and needs to be relinquished so that the same can be used for another request.This is the reason http is the protocol used to access the web application in any web technology.

With the advantage of releasing the bandwidth comes the issue of maintaining the session in J2EE ,as after the request response cycle,the server does not remember the client.

So some extra work is needed in order to maintain the session.

The code to get a session object in servlet is as follows :





The difference between the two overloaded functions is that the function with ‘ TRUE’ as parameter returns a new session object even if a session does not  exist otherwise it will return the preexisting session object but in the later case the function will return the existing session object if it exists or if not existing a null  will be returned.

As we have got the session in a servlet now,it will be lost after a request response cycle.So,in order to maintain session we need to follow either of the approaches from:

1. Hidden parameters

2. Cookies

3. URL rewriting

1. Hidden parameter : In this case we pass the session id allotted by the server to the session object via parameters

2. Cookies : In this case the browser downloads the cookies at the client side,so that it can append the session id with each request.Creating a cookie ,attaching session id to it and sending it to the client side is all done implicitly by the server as soon as we execute the code



3. URL rewriting is something to fall upon if the browser at the client side doe snot accept cookies.In such cases we encode the URL with an extra info called sessionid as the parameter.

The code for url rewriting is as follows :



Number of View :6238

Tags: , , , , , , , ,

  1. No comments yet.

Content Protected Using Blog Protector By: PcDrome.