filter过滤器
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d61bdf9bbc71da6dcb345003b2cbf78f.png)
@WebFilter("/*")
public class UserSessionFilter implements Filter {
private String[] furl = new String[]{"/login", "/zhuce"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
for (int i = 0; i < furl.length; i++) {
String cp = req.getContextPath() + furl[i];
String rui = req.getRequestURI();
if (cp.equals(rui)) {
filterChain.doFilter(req, resp);
return;
}
}
HttpSession session = req.getSession();
Object id = session.getAttribute("isDeng");
if (id == null || "".equals(id)) {
resp.sendRedirect("login");
return;
}
filterChain.doFilter(req, resp);
}
}