1.web.xml
<filter> <filter-name>AdminLoginFilter</filter-name> <filter-class>cn.itcast.goods.admin.web.filter.AdminLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>AdminLoginFilter</filter-name> <url-pattern>/adminjsps/admin/*</url-pattern> <url-pattern>/admin/*</url-pattern> </filter-mapping>
2.AdminLoginFilter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; Object admin = req.getSession().getAttribute("admin"); if(admin == null) { request.setAttribute("msg", "您还没有登录,不要瞎遛达!"); request.getRequestDispatcher("/adminjsps/login.jsp").forward(request, response); } else { chain.doFilter(request, response); } }