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); } }