使用HttpServletRequestWrapper重写Request请求参数

知识养成了思想,思想同时又在融化知识。这篇文章主要讲述使用HttpServletRequestWrapper重写Request请求参数相关的知识,希望能为你提供帮助。
目的: 改变请求参数的值,满足项目需求(如:过滤请求中 lang != zh 的请求)
方法: 1.使用 HttpServletRequestWrapper重写

1 public class ChangeRequestWrapper extends HttpServletRequestWrapper { 2private Map< String, String[]> parameterMap; // 所有参数的Map集合 3 4public ChangeRequestWrapper(HttpServletRequest request) { 5super(request); 6parameterMap = request.getParameterMap(); 7} 8 9// 重写几个HttpServletRequestWrapper中的方法 10 11/** 12* 获取所有参数名 13* 14* @return 返回所有参数名 15*/ 16@Override 17public Enumeration< String> getParameterNames() { 18Vector< String> vector = new Vector< String> (parameterMap.keySet()); 19return vector.elements(); 20} 21 22/** 23* 获取指定参数名的值,如果有重复的参数名,则返回第一个的值 接收一般变量 ,如text类型 24* 25* @param name 指定参数名 26* @return 指定参数名的值 27*/ 28@Override 29public String getParameter(String name) { 30String[] results = parameterMap.get(name); 31return results[0]; 32} 33 34 35/** 36* 获取指定参数名的所有值的数组,如:checkbox的所有数据 37* 接收数组变量 ,如checkobx类型 38*/ 39@Override 40public String[] getParameterValues(String name) { 41return parameterMap.get(name); 42} 43 44@Override 45public Map< String, String[]> getParameterMap() { 46return parameterMap; 47} 48 49public void setParameterMap(Map< String, String[]> parameterMap) { 50this.parameterMap = parameterMap; 51} 52 }

 

2.新增Filter

1 public class LangFilter implements Filter { 2@Override 3public void init(FilterConfig filterConfig) throws ServletException { 4 5} 6 7@Override 8public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 9 10ChangeRequestWrapper changeRequestWrapper = new ChangeRequestWrapper((HttpServletRequest) servletRequest); 11 12Map< String, String[]> parameterMap = new HashMap< > (changeRequestWrapper.getParameterMap()); 13 14String[] strings = parameterMap.get("lang");
//逻辑代码给定默认的参数值,如果参数不为中文,则直接返回异常 15if (strings == null || strings.length == 0) { 16strings = new String[1]; 17strings[0] = "zh"; 18parameterMap.put("lang", strings); 19changeRequestWrapper.setParameterMap(parameterMap); 20}else{ 21String language = strings[0]; 22if (!language.equals("zh")) { 23Map< String,String> error = new HashMap< > (); 24error.put("code","500"); 25 26ServletOutputStream outputStream = null; 27try { 28outputStream = servletResponse.getOutputStream(); 29outputStream.write(JSONUtils.obj2Byte(error)); 30} finally { 31if (outputStream != null) { 32outputStream.flush(); 33outputStream.close(); 34} 35} 36 37return; 38} 39} 40//使用复写后的wrapper 41filterChain.doFilter(changeRequestWrapper, servletResponse); 42} 43 44@Override 45public void destroy() { 46 47} 48 }

      3.web.xml增加一个过滤器处理
1< filter> 2< filter-name> languageFilter< /filter-name> 3< filter-class> com.intercepor.LangFilter< /filter-class> 4< init-param> 5< param-name> encoding< /param-name> 6< param-value> UTF-8< /param-value> 7< /init-param> 8< init-param> 9< param-name> forceEncoding< /param-name> 10< param-value> true< /param-value> 11< /init-param> 12< /filter> 13 14< filter-mapping> 15< filter-name> languageFilter< /filter-name> 16< url-pattern> *.do< /url-pattern> 17< /filter-mapping>

所有.do的请求,都会验证此参数
【使用HttpServletRequestWrapper重写Request请求参数】 
 




    推荐阅读