protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8"); //在这加一行设置编码的的代吗。
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.processPreprocess(request, response);
}
}
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Iterator iter=request.getParameterMap().values().iterator();
while(iter.hasNext())
{
String[] parames=(String[])iter.next();
for (int i = 0; i < parames.length; i++) {
try {
parames[i]=new String(parames[i].getBytes("iso8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
FilterConfig filterConfig;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
MyRequest req = new MyRequest( (HttpServletRequest)request);//设置代理请求
filterChain.doFilter(req, response);
}
this.filterConfig = arg0;
}
}
class MyRequest extends javax.servlet.http.HttpServletRequestWrapper{//自定义的代理请求类
HttpServletRequest request;
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
public String[] getParameterValues(String name){
String strs[] = super.getParameterValues(name);
for(int i=0;i<strs.length;i++){
strs[i] = this.myEncoding(strs[i]);
}
return strs;
}
private String myEncoding(String input){
String output ="";
try {
output = new String(input.getBytes("ISO-8859-1"),"UTF-8"); //注意同页面的编码保持一致
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
}
}
<filter-name>encoding</filter-name>
<filter-class>com.cao.struts.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>