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

MyF*_*ist 1 servlets jsf-2

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

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

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

sku*_*sel 5

在servlet中,请求/会话/应用程序属性是来自doGet(HttpServletRequest request, HttpServletResponse response)/ doPost(HttpServletRequest request, HttpServletResponse response)methods 中的availbale :

//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");
Run Code Online (Sandbox Code Playgroud)

当请求由the处理时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");
Run Code Online (Sandbox Code Playgroud)

建议阅读