下面的代码片段就是我正在使用的内容。
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> sessionMap = externalContext.getSessionMap();
sessionMap.put("User",user);
现在我如何从普通的“servlet”获得“sessionMap” - “key”值?
像(User)session.getAttribute("User");
这样的代码是否可以通过我的servlet工作?
答案 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");