一:Filter过滤器。
先自定义一个过滤器。
1 package com.jbj.filter; 2 3 import org.springframework.web.filter.OncePerRequestFilter; 4 5 import javax.servlet.FilterChain; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 public class BuilderFilter extends OncePerRequestFilter { 12 protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { 13 System.out.println("测试filter"); 14 filterChain.doFilter(httpServletRequest, httpServletResponse); 15 System.out.println("############TestFilter1 doFilter after############"); 16 } 17 }
要在web.xml中进行配置。
<filter> <filter-name>BuildFilter</filter-name> <filter-class>com.jbj.filter.BuilderFilter</filter-class> </filter> <filter-mapping> <filter-name>BuildFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
输出的结果。现在这个是配置了拦截器。
这一点错了。过滤器和拦截器都是先进后出的。看上图的整个流程。
二:过滤器的用途
前台传递的数据可能有空格什么的。非法的字符。先进过滤器的doFilter前进行处理。写一个类。去继承request。然后修改request里的值。doFilter去执行写的这个类。具体实现如下:
1 package com.jbj.filter; 2 3 import org.springframework.web.filter.OncePerRequestFilter; 4 import javax.servlet.FilterChain; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletRequestWrapper; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 public class BuilderFilter extends OncePerRequestFilter { 12 protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { 13 System.out.println("测试filter"); 14 ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(httpServletRequest); 15 filterChain.doFilter(requestWrapper, httpServletResponse); 16 System.out.println("############TestFilter1 doFilter after############"); 17 } 18 }
写ParameterRequestWrapper类去继承 HttpServletRequestWrapper类。然后重新需要修改的方法就行了。
package com.jbj.filter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class ParameterRequestWrapper extends HttpServletRequestWrapper { private Map<String , String[]> params = new HashMap<String, String[]>(); @SuppressWarnings("unchecked") public ParameterRequestWrapper(HttpServletRequest request) { // 将request交给父类,以便于调用对应方法的时候,将其输出,其实父亲类的实现方式和第一种new的方式类似 super(request); //将参数表,赋予给当前的Map以便于持有request中的参数 this.params.putAll(request.getParameterMap()); System.out.println("aa"); this.modifyParameterValues(); System.out.println("bb"); } //重载一个构造方法 public ParameterRequestWrapper(HttpServletRequest request , Map<String , Object> extendParams) { this(request); addAllParameters(extendParams);//这里将扩展参数写入参数表 } /** * 将parameter的值去除空格后重写回去 */ public void modifyParameterValues(){ Set<String> set =params.keySet(); Iterator<String> it=set.iterator(); while(it.hasNext()){ String key= (String) it.next(); String[] values = params.get(key); //.trim()只是去除头尾的空格。replaceAll去除所有的空格 values[0] = values[0].replaceAll(" ", ""); params.put(key, values); } System.out.println("cc"); } @Override public String getParameter(String name) {//重写getParameter,代表参数从当前类中的map获取 String[]values = params.get(name); if(values == null || values.length == 0) { return null; } return values[0]; } public String[] getParameterValues(String name) {//同上 return params.get(name); } public void addAllParameters(Map<String , Object>otherParams) {//增加多个参数 for(Map.Entry<String , Object>entry : otherParams.entrySet()) { addParameter(entry.getKey() , entry.getValue()); } } public void addParameter(String name , Object value) {//增加参数 if(value != null) { if(value instanceof String[]) { params.put(name , (String[])value); }else if(value instanceof String) { params.put(name , new String[] {(String)value}); }else { params.put(name , new String[] {String.valueOf(value)}); } } } }