Saturday, January 21, 2012

Set and get object into/from session

/**
* Method for setting an object in session
* @param name
* @param value
* @return null
*/
public static void setSessionValue(String name, Object value) {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request =
(HttpServletRequest)context.getExternalContext().getRequest();
HttpSession httpSession = request.getSession(false);
httpSession.setAttribute(name, value);
}
/**
* Method for getting an object from session
* @param null
* @return Object
*/
public static Object getSessionValue(String attName) {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request =
(HttpServletRequest)context.getExternalContext().getRequest();
HttpSession httpSession = request.getSession(false);
return httpSession.getAttribute(attName);
}

No comments:

Post a Comment