java参数传递的乱码问题
编码设置的几种情况:
(1)页面里统一使用utf-8格式
<%@ page contentType="text/html;charset=UTF-8"%>
(2)在提交之前对URL进行转码处理:
encodeURI(encodeURL(url))要用两次的encodeURI进行转码
在接收参数时进行解码:
URLDecoder.decode(request.getParam(“param”),”UTF-8”);
(3) 接受参数时进行编码转换
testString =new String(testString.getBytes(”ISO-8859-1〃),”utf-8〃)
这样的话,每一个参数都必须这样进行转码
(4) 在请求页面上开始处,设置请求的编码
request.setCharacterEncoding("UTF-8")把提交内容的字符集设为UTF-8,这样的话,接受此参数的页面就不必在转码了。
(5)修改tomcat的server.xml
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8"/>
(6)写一个编码过滤器
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)arg0;
req.setCharacterEncoding("utf-8");
arg2.doFilter(arg0,arg1);
}
欢迎加入技术讨论群101085864