我试图通过我的页面保持 SessionBean 范围,为此,我从这里遵循了一些教程,实际上,我试图通过 ExternalContext 获取会话,如下面的代码:
public class LoginFilter implements Filter{
ProfileBean pBean = new ProfileBean();
ActiveUserModel activeUserModel;
ExternalContext tmpEC;
Map sMap;
public void destroy() {}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
tmpEC = FacesContext.getCurrentInstance().getExternalContext();
sMap = tmpEC.getSessionMap();
activeUserModel = (ActiveUserModel) sMap.get("ActiveUserModel");
String username = SecurityAssociation.getPrincipal().getName();
if(activeUserModel.getUsername() == null)
{
try {
pBean.consultaProfile(username);
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}else{
}
filterChain.doFilter(servletRequest, servletResponse);
}
public void init(FilterConfig filterConfig) throws ServletException {}
}
在这一行 if(activeUserModel.getUsername() == null),我得到一个 java.lang.NullPointerException 因为我没有实例化 bean,但即使我实例化它,也没有工作。
有什么不对吗?