在web.xml里面配置,有两种形式:
然后编写filter类:
@Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)arg0; HttpServletResponse response = (HttpServletResponse)arg1; HttpSession session = request.getSession(); if(session.getAttribute("admin") == null && request.getRequestURL().indexOf("/admin/login") == -1) { response.sendRedirect(request.getContextPath()+"/admin/login"); }else { arg2.doFilter(arg0, arg1); } } @Override public void init(FilterConfig arg0) throws ServletException { //@Description: TODO }