07 June, 2012

Setter and Getter of Session Parameters in ADF


We usually use session parameters as global variables that is still alive for entire session.

I will produce today the getter and setter of session parameters.

Setter of Session Parameters 
   public void setSessionParameter(String name, Object value) {  
     HttpServletRequest request =  
       (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();  
     HttpSession session = request.getSession(false);  
     session.setAttribute(name, value);  
   }  

Getter of Session Parameters
   public Object getSessionParameter(String name) {  
     HttpServletRequest request =  
       (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();  
     HttpSession session = request.getSession(false);  
     return session.getAttribute(name);  
   }  



Don't forget to import below classes in your custom class
 import javax.faces.context.FacesContext;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpSession;  

If you have your custom framework for
getter of FacesContext
getter of ExternalContext
getter of Session

then you should replace my code to get these objects by your methods you use in your custom framework.

Thanks
Mahmoud A. El-Sayed
Recommended Post Slide Out For Blogger