1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.white.filter;
import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException;
public class MangerFilter implements Filter {
@Override public void init(FilterConfig filterConfig) throws ServletException {
}
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; Object user = httpServletRequest.getSession().getAttribute("username"); if (user == null){ httpServletRequest.getRequestDispatcher("index.jsp").forward(servletRequest,servletResponse); System.out.println("已拦截请求"); }else{ filterChain.doFilter(servletRequest,servletResponse); }
}
@Override public void destroy() {
} }
|