1

Below code snippet is what I'm using.

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
  Map<String, Object> sessionMap = externalContext.getSessionMap();
  sessionMap.put("User",user);

Now how can I get above "sessionMap" - "key" value from a plain "servlet"? Will the code like this (User)session.getAttribute("User"); work by any chance from my servlet?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MyFist
  • 413
  • 7
  • 19

1 Answers1

5

Within a servlet the request / session / application attributes are availbale from within doGet(HttpServletRequest request, HttpServletResponse response) / doPost(HttpServletRequest request, HttpServletResponse response) methods:

//request attributes
String string = (String)request.getAttribute("username");
//session attributes
String string = (String)request.getSession().getAttribute("username");
//application attributes
String string = (String)getServletContext().getAttribute("beanName");

When the request was handled by the FacesServlet, the attributes are available as:

//request attributes
String string = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("username");
//session attributes
String string = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("username");
//application attributes
String string = (String)FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("username");

Suggested reading

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67