方法(一):
html页面:
function testOne() {
var url = "testOne_test.do?expr="+你好;
location = encodeURI(url);
}
后台java代码:
String expr = new String(request.getParameter("expr").getBytes("ISO-8859-1"),"UTF-8");
方法(二):
html页面:
function testOne() {
var url = "testOne_test.do?expr="+你好;
location = encodeURI(encodeURI(url));
}
后台java代码:
String expr = java.net.URLDecoder.decode(lrequest.getParameter("expr") , "UTF-8");
在tomcat下面找到server.xml
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" URIEncoding="GBK">
根据需要修改为UTF-8等字符集。
<filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
org.springframework.web.filter.CharacterEncodingFilter 中的转码部分:
protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding && responseSetCharacterEncodingAvailable) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); }
参考:
url:http://blog.163.com/wkyuyang_001/blog/static/108021228200971352434158/
Spring源文件