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 :
request.getSession(TRUE);
or
request.getSession(FALSE);
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
request.getSession(TRUE/FALSE);
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 :
response.encodeRedirectUrl("url-pattern");
Number of View :5625
Related posts:
- How To Redirect From One Servlet To Another Servlet Basics Servlet Redirect
- How To Get The Parameters From One Servlet To Another Using Request
- Difference Between Parameters And Attributes In Servlets/JSP Parameter vs. Attribute
- A Hello,World Servlet Servlet Tutorial Servlet Examples Run servlet on tomcat
- how to include one servlet into another Servlet Essentials Servlet Include
Tags: cookies, getSesson(), hidden fields, http stateless protocol, j2ee, java, session amangement, sessions, url rewriting
This entry was posted on Tuesday, May 8th, 2012 at 4:01 am and is filed under Java, software, Tech-Tips, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.