public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
//进入服务器之前
System.out.println("进入服务器之前>>>>");
HttpServletRequest req=(HttpServletRequest) arg0;
req.setCharacterEncoding(encoding);
if(req.getSession().getAttribute("user")==null){
HttpServletResponse res=(HttpServletResponse) arg1;
res.sendRedirect("login.jsp");
}else{
arg2.doFilter(arg0, arg1);
String path=req.getRequestURI();
System.out.println("你请求的路径是:"+path);
System.out.println("<<<<<进入服务器之后");
}
//进入服务器之后
}
public class MyFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("进入Filter");
arg2.doFilter(arg0, arg1);//----->
System.out.println("离开Filter");
}
public void init(FilterConfig arg0) throws ServletException {
System.out.println("初始化");
}
}
原文:http://www.cnblogs.com/tian114527375/p/4937639.html