我正在使用我编写的过滤器进行会话管理,并使用 managedbean 注释我试图将验证成功发送到 filter 。
@ManagedBean(name = "customer")
@SessionScoped
public class CustomerBean implements Serializable{
public String checkValidCustomer(){
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
HttpSession session = request.getSession(true);
Customer cust = new Customer();
cust.setUsername(getUsername());
cust.setPassword(getPassword());
String returnValue = customerBo.checkValidCustomer(cust);
if(returnValue == "success"){
session.setAttribute("customer", returnValue);
}
else
{
session.setAttribute("customer", null);
}
return returnValue;
我在 retunValue 字符串中获得了成功,所以我尝试将值作为值传递给 managedBean 注释。myfilter 代码就像。
@WebFilter("/faces/*")
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String cust = (String) req.getSession().getAttribute("customer");
if (cust != null && cust.equals("success")) {
chain.doFilter(request, response);
} else {
resp.sendRedirect(req.getContextPath() + "/faces/default.xhtml");
}
}
我得到 String cust 的空值,当我打开登录页面时,它显示这个网页有一个重定向循环。你能告诉我哪里做错了吗?