如何从servlet获取JSF2.0 sessionMap引用

时间:2013-02-24 16:46:25

标签: servlets jsf-2

下面的代码片段就是我正在使用的内容。

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

现在我如何从普通的“servlet”获得“sessionMap” - “key”值? 像(User)session.getAttribute("User");这样的代码是否可以通过我的servlet工作?

1 个答案:

答案 0 :(得分:5)

在servlet中,doGet(HttpServletRequest request, HttpServletResponse response) / doPost(HttpServletRequest request, HttpServletResponse response)方法中的请求/会话/应用程序属性是可用的:

//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");

FacesServlet处理请求时,属性可用:

//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");

建议阅读