1

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

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

现在我怎样才能从普通的“servlet”中获得高于“sessionMap”-“key”的值?像这样的代码会在(User)session.getAttribute("User");我的 servlet 中运行吗?

4

1 回答 1

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

推荐阅读

于 2013-02-25T11:05:12.377 回答