1、构建request扩展类
package com.yuchengtech.emp.dataquality.filter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class RequestWrapper extends HttpServletRequestWrapper {
private Map<String, String[]> params = new HashMap<String, String[]>();
public RequestWrapper(HttpServletRequest request) {
super(request);
//将现有parameter传递给params
this.params.putAll(request.getParameterMap());
}
/**
* 重载构造函数
* @param request
* @param extraParams
*/
public RequestWrapper(HttpServletRequest request, Map<String, String[]> extraParams) {
this(request);
addParameters(extraParams);
}
public void addParameters(Map<String, String[]> extraParams) {
for (Map.Entry<String, String[]> entry : extraParams.entrySet()) {
addParameter(entry.getKey(), entry.getValue());
}
}
/**
* 重写getParameter,代表参数从当前类中的map获取
* @param name
* @return
*/
@Override
public String getParameter(String name) {
String[]values = params.get(name);
if(values == null || values.length == 0) {
return null;
}
return values[0];
}
/**
* 同上
* @param name
* @return
*/
@Override
public String[] getParameterValues(String name) {
return params.get(name);
}
/**
* 添加参数
* @param name
* @param value
*/
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)});
}
}
}
}
2、创建过滤器,修改request中的值
package com.yuchengtech.emp.dataquality.filter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yuchengtech.emp.utils.FilterUtil;
public class EncodeHtmlFilter implements Filter {
public static final Logger log = LoggerFactory.getLogger(EncodeHtmlFilter.class);
public void init(FilterConfig filterConfig) throws ServletException {
log.info("filter init");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
log.info("doFilter begin");
Map<String, String[]> map = request.getParameterMap();
Map<String, String[]> mapNew = new HashMap<String, String[]>();
if (map != null) {
for (Map.Entry<String, String[]> entry : map.entrySet()) {
String[] arr = entry.getValue();
if (arr.length > 0 && arr != null) {
arr[0] = FilterUtil.encodeHtml(arr[0].toString());
}
mapNew.put(entry.getKey(), arr);
}
}
RequestWrapper requestParameterWrapper = new RequestWrapper((HttpServletRequest)request);
requestParameterWrapper.addParameters(mapNew);
chain.doFilter(requestParameterWrapper, response);
log.info("doFilter end");
}
public void destroy() {
log.info("filter destroy");
}
}
3、配置web.xml
<filter>
<filter-name>encodeHtmlFilter</filter-name>
<filter-class>
com.yuchengtech.emp.dataquality.filter.EncodeHtmlFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>encodeHtmlFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>