重写request对象
public class CustomHttpServletRequest extends HttpServletRequestWrapper {
protected Logger logger = Logger.getLogger(CustomHttpServletRequest.class);
private static final String ENCODED = "UTF-8", DECODE = "ISO8859-1";
public CustomHttpServletRequest(HttpServletRequest request) {
super(request);
}
@Override
public String getQueryString() {
String queryString = super.getQueryString();
if (queryString != null) {
try {
return new String(queryString.getBytes(DECODE), ENCODED);
} catch (UnsupportedEncodingException e) {
logger.info(e.getMessage(), e);
}
}
return queryString;
}
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
if (value != null) {
try {
return new String(value.getBytes(DECODE), ENCODED);
} catch (UnsupportedEncodingException e) {
logger.info(e.getMessage(), e);
}
}
return value;
}
@SuppressWarnings("unchecked")
@Override
public Map getParameterMap() {
Map<String, String[]> values = super.getParameterMap();
Map<String, String[]> valuesMap = new HashMap<String, String[]>();
Set<String> names = values.keySet();
for (Iterator<String> iter = names.iterator(); iter.hasNext();) {
String name = iter.next();
String[] v = values.get(name);
if (v != null) {
String[] valuesCopy = new String[v.length];
for (int i = 0; i < v.length; i++) {
try {
valuesCopy[i] = new String(v[i].getBytes(DECODE),
ENCODED);
} catch (UnsupportedEncodingException e) {
logger.info(e.getMessage(), e);
}
}
valuesMap.put(name, valuesCopy);// 向新Map放入转码过的值
}
}
return valuesMap;// 返回是副本
}
@Override
public String[] getParameterValues(String name) {
String[] values = super.getParameterValues(name);
if (values != null) {
String[] valuesCopy = new String[values.length];// 原数组是只读的,不能修改
for (int i = 0; i < values.length; i++) {
try {
valuesCopy[i] = new String(values[i].getBytes(DECODE),
ENCODED);
} catch (UnsupportedEncodingException e) {
logger.info(e.getMessage(), e);
}
}
return valuesCopy;
}
return values;
}
}
servlet里使用
H2WHttpServletRequest request = new H2WHttpServletRequest(req);