更改Request Parameters中的值

1. 定义ParameterRequestWrapper 继承HttpServletRequestWrapper

public class ParameterRequestWrapper extends HttpServletRequestWrapper {

    private Map<String,String[]> params;

    public ParameterRequestWrapper(HttpServletRequest request, Map<String,String[]> newParams) {
super(request);
this.params = newParams;
} @Override
public String getParameter(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
String[] strArr = (String[]) v;
if (strArr.length > 0) {
return strArr[0];
} else {
return null;
}
} else if (v instanceof String) {
return (String) v;
} else {
return v.toString();
}
} @Override
public Map getParameterMap() {
return params;
} @Override
public Enumeration getParameterNames() {
return new Vector(params.keySet()).elements();
} @Override
public String[] getParameterValues(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
return (String[]) v;
} else if (v instanceof String) {
return new String[] { (String) v };
} else {
return new String[] { v.toString() };
}
} }

2. 在filter 中编写如下代码:

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
Map<String, String[]> parametersMap = DataFormat
.formatRequestParameters(httpRequest);
ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(
httpRequest, parametersMap);
chain.doFilter(requestWrapper, response);
}
上一篇:android开发系列之socket编程


下一篇:HDU 5860 Death Sequence(递推)