一:filter:过滤器,拦截servlet的请求和响应。
1、
1 package jd.com.filter; 2 3 import javax.servlet.*; 4 import java.io.IOException; 5 6 public class MyFilter implements Filter { 7 @Override 8 public void destroy() { 9 10 } 11 12 @Override 13 public void init(FilterConfig filterConfig) { 14 15 } 16 17 @Override 18 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) { 19 20 try { 21 System.out.println("请求到拦截器。"); 22 //放行请求和响应。 23 filterChain.doFilter(servletRequest,servletResponse); 24 System.out.println("响应到拦截器。"); 25 }catch (Exception ex){ 26 ex.printStackTrace(); 27 throw new RuntimeException(ex+"run ereror"); 28 } 29 30 } 31 }