#HttpSession API (Working with javax.servlet.http.HttpSession):
1) To create HttpSession object or to get access to an existing HttpSession object:
a) HttpSession session=request.getSesssion();
This method creates new HttpSession Object on the server for browser window if HttpSession object not already available for that browser window on the server otherwise this method provides access to existing session object of browser window.
This method can create new session between browser window and web application if session is not already there otherwise it makes current request participating in the existing session i.e. already started between browser window and web application.
b) HttpSession session=request.getSesssion(false);
This method gives access to existing HttpSession object of browser window if HttpSession object is already available on the server for that browser window otherwise returns null (i.e. no new HttpSession object will be created on the server for browser window).
This method makes the browser window generated request(client) to participate in the existing session that is already started between browser window and web application but it can not create new session between browser window and web application.
c) HttpSession session=request.getSesssion(true);
(Same is above option a i.e )
This method creates new HttpSession Object on the server for browser window if HttpSession object not already available for that browser window on the server otherwise this method provides access to existing session object of browser window.
This method can create new session between browser window and web application if session is not already there otherwise it makes current request participating in the existing session i.e. already started between browser window and web application.
javax.servlet.http.HttpSession is interface :
HttpSession object means it is the object of underlaying server supplied java class that implements javax.servlet.http.HttpSession.
Create new Session Attributes:
------------------------------
Use session.setAttribute(-,-) OR session.putValue(-,-) methods
session.setAttribute("name","Shruti");
session.setAttribute("age",new Integer("15"));
OR
session.putValue("state","Maharashtra");
session.putValue("college","CSMSS, CSCOE, Aurangabad");
session.putValue("dteCode",new Integer("2533"));
Modify existing session attribute values:
-----------------------------------------
Use session.setAttribute(-,-) OR putValue(-,-) methods
session.setAttribute("name","bhosale");
session.setAttribute("age",new Integer("4"));
OR
session.putValue("state","Andhra Pradesh");
session.putValue("college","CSMSS, chh. shahu college of Engg");
Read existing session attributes values:
----------------------------------------
Use session.getAttribute(-) or session.getValue(-) methods
String name=(String)session.getAttribute("name");
int age=(Integer)session.getValue("age");
Remove Existing session attributes:
----------------------------------
Use session.removeValue(-) OR sesssion.removeAttribute(-) methods
session.removeAttribute("name");
session.removeValue("college");
Following methods are deprecated methods form Servlet-API2.2:
removeValue(-)
getValue(-)
putValue(-,-)