如果用户已经登录,如何将用户重定向到主页。我使用过滤器类登录页面,但它不能正常工作。我的代码是:
@WebFilter(filterName = "loginFilter",
urlPatterns ={"/login.xhtml"})
public class LoginFilter implements Filter{
private FilterConfig filterconfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterconfig = filterconfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httprequest =(HttpServletRequest) request;
HttpServletResponse httpresponse =(HttpServletResponse) response;
try{
if(httprequest.getUserPrincipal() != null){
System.out.printf("User authenticated with " + httprequest.getRemoteUser() + " username conected.");
httprequest.getRequestDispatcher("/home.xhtml").forward(request, response);
} else{
chain.doFilter(request, response);
}
}catch(Exception){
//do something
}
}
@Override
public void destroy() {
System.out.print("Existing from loginFilter");
}
}
这是我的问题:用户登录到网络并执行一些导航和操作,然后使用浏览器的后退按钮返回登录页面,无需注销,再次输入用户名密码,然后按登录按钮。然后它抛出异常IndexOutofBoundsException。我只需要检查用户何时使用链接或浏览器的后退按钮导航到登录页面,然后重定向到主页。有什么建议吗?