@WebFilter(urlPatterns = {"/*"}) public class LoginFiilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest servletRequest1=(HttpServletRequest) request; //放行URL if (servletRequest1.getRequestURI().equals("/user/login")){ filterChain.doFilter(request,response); } Object user = servletRequest1.getSession().getAttribute("user"); if (user==null){ servletRequest1.getRequestDispatcher("/login.jsp").forward(request,response); }else { //放行请求 filterChain.doFilter(request,response); } } @Override public void destroy() { } }