*** ***
根本java中文问题的解决之道是统一编码为UTF-8
1)页面中文硬编码输出:
使用JSTL标签(也可用到structs中,),所有输出都是通过message.perporyt文件定义,基本不再出现本地化字符在客户端的页面上
2)中文数据存储:
统一在hibernate的jdbc字符串中注释为utf-8,
如jdbc:mysql://localhost:3306/hgoa?useUnicode=true&characterEncoding=utf-8
3)页面输入中文的角本编译:
(a)用页面首语句统一编码
<%@ page language="java" pageEncoding="utf-8" %>
(b)在web.xml中加拦截器(这里是用的spring的一个拦截器)
<filter>
<filter-name>Set CharacterEncoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
3)服务器端的设置:
tomcat的server.xml,设置8080端口的容器中属性为
(其中,useBodyEncodingForURI="true" 是关键句):
<Connector
useBodyEncodingForURI="true"
port="8080"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="20000"
maxSpareThreads="75"
maxThreads="150"
maxHttpHeaderSize="8192">
</Connector>
4)页面间的中文参数传递:
将URL里的中文参数进行字符串编码,成为Get可以识别的串,%+"ASC码" , URL传递的时候,接收时,会自 ,而从根本上解决传递中文时,不出问题
例如引用urlencode.js中的方法UrlEncode(str)
<script src="/query/jslib/urlencode.js"></script>
<script>
function sub(){
var productname=new String();
productname=getValue('fsearch','productname');
top.down.location='searchProductsbyClassTwo.do?productname='+UrlEncode(productname)+'&classid='+<ww:property value="#request.classid"/>;
}
</script>
urlencode.js中:
function UrlEncode(str){
else{
}
return ret;
}
*** ***
以上是我们试验了N多种中文解决之道后,总结出来的最有效最简洁也最彻底的办法(自吹一下)
根本java中文问题的解决之道是统一编码为UTF-8
1)页面中文硬编码输出:
使用JSTL标签(也可用到structs中,),所有输出都是通过message.perporyt文件定义,基本不再出现本地化字符在客户端的页面上
2)中文数据存储:
统一在hibernate的jdbc字符串中注释为utf-8,
如jdbc:mysql://localhost:3306/hgoa?useUnicode=true&characterEncoding=utf-8
3)页面输入中文的角本编译:
(a)用页面首语句统一编码
<%@ page language="java" pageEncoding="utf-8" %>
(b)在web.xml中加拦截器(这里是用的spring的一个拦截器)
<filter>
<filter-name>Set CharacterEncoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
3)服务器端的设置:
tomcat的server.xml,设置8080端口的容器中属性为
(其中,useBodyEncodingForURI="true" 是关键句):
<Connector
useBodyEncodingForURI="true"
port="8080"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="20000"
maxSpareThreads="75"
maxThreads="150"
maxHttpHeaderSize="8192">
</Connector>
4)页面间的中文参数传递:
将URL里的中文参数进行字符串编码,成为Get可以识别的串,%+"ASC码" , URL传递的时候,接收时,会自 ,而从根本上解决传递中文时,不出问题
例如引用urlencode.js中的方法UrlEncode(str)
<script src="/query/jslib/urlencode.js"></script>
<script>
function sub(){
var productname=new String();
productname=getValue('fsearch','productname');
top.down.location='searchProductsbyClassTwo.do?productname='+UrlEncode(productname)+'&classid='+<ww:property value="#request.classid"/>;
}
</script>
urlencode.js中:
function UrlEncode(str){
var i,c,ret="",strSpecial="!/"#$%&'()*+,/:;<=>?@[/]^`{|}~%";
for(i=0;i<str.length;i++){
if(str.charCodeAt(i)>=0x4e00){
c=qswhU2GB[str.charCodeAt(i)-0x4e00];
ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
}for(i=0;i<str.length;i++){
if(str.charCodeAt(i)>=0x4e00){
c=qswhU2GB[str.charCodeAt(i)-0x4e00];
ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
else{
c=str.charAt(i);
if(c==" ")
ret+="+";
else if (c=="/r")
ret+="%0d";
else if (c=="/n")
ret+="%0a";
else if(strSpecial.indexOf(c)!=-1)
ret+="%"+str.charCodeAt(i).toString(16);
else
ret+=c;
}if(c==" ")
ret+="+";
else if (c=="/r")
ret+="%0d";
else if (c=="/n")
ret+="%0a";
else if(strSpecial.indexOf(c)!=-1)
ret+="%"+str.charCodeAt(i).toString(16);
else
ret+=c;
}
return ret;
}
*** ***
以上是我们试验了N多种中文解决之道后,总结出来的最有效最简洁也最彻底的办法(自吹一下)