Saturday, November 21, 2009

JSF 1.2 : Session

How to Create Session ?
-----------------

FacesContext fctx=FacesContext.getCurrentInstance();
HttpServletRequest req = (HttpServletRequest)fctx.getExternalContext().getRequest();
HttpSession session=req.getSession();

How to Set the Value into Session ?
------------------------------
session.setAttribute("userid", login); /// where login is the value which comes from the login screen
session.setAttribute("password", pwd); /// where password is the value which comes from the login screen
System.out.println("Login/Password set in to Session...");


How to Get the Value from Session ?
--------------------------------

String u = (String)session.getAttribute("userid"); //getting arrribute from session
System.out.println("UserName from Session-> "+u);
String p = (String)session.getAttribute("password");//getting arrribute from session
System.out.println("Password from Session-> "+p);

How to Invalidate the Session ?
---------------------

HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.invalidate();