在做java开发的过程中一开始就碰到中文问题,刚接触到java时用的编译器是JCreate,这是个非常好用简单的java编译器,
但他就在对中文的支持上有很大问题,当时我就在想中国这么大的国家为什么在世界上总是比不过那些小国家,最严重的是有日文开发文档
就是没有中文的,所以我们在开发java的时候总是会碰到中文问题。下面我就来说一下我在开发中是如何来处理中文的。
      首先讲中文的字符的编码,刚开始中文的编码是GB2312后来发现他的常用字根本不能适应现在的需求,所以就在他的基础上增加了很多
生僻字,不常用字得到了新的编码GBK,我们一般来说都用GBK来实现我们的开发,而如果说到国际化就不得不提Unicode (统一码)顾名思
义是一个将世界上各种文字统一在一起的东东。由美国各大电脑厂商组成的Unicode策进会来推动。目的,推广一个世界通用的编码体制,
惊世界上所有常用的文字都涵盖进去,从而减少个电脑商开发国外市场遇到的问题。
        这些编码系统也会互相冲突。也就是说,两种编码可能使用相同的数字代表两个不同的字符,或使用不同的数字代表相同的字符。任何
一台特定的计算机(特别是服务器)都需要支持许多不同的编码,但是,不论什么时候数据通过不同的编码或平台之间,那些数据总会有损坏
的危险。
    Unicode给每个字符提供了一个唯一的数字,不论是什么平台,不论是什么程序,不论什么语言。Unicode标准已经被这些工业界的领导
们所采用,例如:Apple, HP, IBM, JustSystem, Microsoft, Oracle, SAP, Sun, Sybase, Unisys和其它许多公司。最新的标准都需要
Unicode,例如XML, Java, ECMAScript (JavaScript), LDAP, CORBA 3.0, WML等等,并且,Unicode是实现ISO/IEC 10646的正规方式。许多
操作系统,所有最新的浏览器和许多其他产品都支持它。Unicode标准的出现和支持它工具的存在,是近来全球软件技术最重要的发展趋势。
       而Unicode4.0后有了UTF (Unicode/UCS Transformation Format),Unicode推荐使用UTF-8和UTF-16两种格式其中8和16指的是Bits数而不
是Bytes数。
UTF-16基本就是Unicode双字节的实现,加上一个应付未来需要的扩充编码机制(很少用)
UTF-8 是一种不等幅的编码方式,英数字(Ascii字码)保持原状,完全不受影响(因此不需要做转换),而其他汉字资料须透过程序来转换,
会[变胖],因为每个字需要额外一个或两个Bytes来编码。
       下面我们来看看实战中我是如何做的:
    1.web.xml
 配置过滤器
 
		
				1
				 <!--
				 Filter Configuration 
				-->
				<!--
				 Filter Configuration 
				-->
				
						
				
				2
				
						 <
				filter
				>
 
				<
				filter
				>
				
						
				
				3
				
						 <
				filter
				-
				name
				>
				Set Character Encoding
				</
				filter
				-
				name
				>
  
				<
				filter
				-
				name
				>
				Set Character Encoding
				</
				filter
				-
				name
				>
				
						
				
				4
				
						 <
				filter
				-
				class
				>
				cn.redstoneinfo.commons.web.EncodingFilter
				</
				filter
				-
				class
				>
  
				<
				filter
				-
				class
				>
				cn.redstoneinfo.commons.web.EncodingFilter
				</
				filter
				-
				class
				>
				  
				5
				
						 </
				filter
				>
 
				</
				filter
				>
				
						
				
				6
				
						 
				
		 
		    
    2.EncodingFilter.java:
		
				 1
				 //
				这里设置的默认的编码,可以在web.xml中设置,如果不设就用这里的
				 
				//
				这里设置的默认的编码,可以在web.xml中设置,如果不设就用这里的
				
						
				
				 2
				
						 protected
				 String encoding 
				=
				 
				"
				UTF-8
				"
				;
				
				 
				protected
				 String encoding 
				=
				 
				"
				UTF-8
				"
				;
				 3
				
						 
						
				
				 4
				
						 protected
				 
				boolean
				 ignore 
				=
				 
				true
				;
 
				protected
				 
				boolean
				 ignore 
				=
				 
				true
				;
				 5
				
						 
						
				
				 6
				
						 
						 /**/
				
						/*
 
				/**/
				
						/*
						 
						 7
						
								 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
						 8
						
								 */
  
						*/
				
				
						
				
				 9
				
						 
						 public
				 
				void
				 init(FilterConfig filterConfig) 
				throws
				 ServletException
 
				public
				 
				void
				 init(FilterConfig filterConfig) 
				throws
				 ServletException 
				
						 {
				
				
						{
						10
						
								 String paramValue 
						=
						 filterConfig.getInitParameter(
						"
						encoding
						"
						);
  String paramValue 
						=
						 filterConfig.getInitParameter(
						"
						encoding
						"
						);
						11
						
								 
								 if
						 (paramValue 
						!=
						 
						null
						)
  
						if
						 (paramValue 
						!=
						 
						null
						) 
						
								 {
						
						
								{
								12
								
										 this
								.encoding 
								=
								 paramValue;
   
								this
								.encoding 
								=
								 paramValue;
								13
								
										 }
  }
						
						
								
						
						14
						
								 String value 
						=
						 filterConfig.getInitParameter(
						"
						ignore
						"
						);
  String value 
						=
						 filterConfig.getInitParameter(
						"
						ignore
						"
						);
						15
						
								 if
						 (value 
						==
						 
						null
						)
  
						if
						 (value 
						==
						 
						null
						)
						16
						
								 this
						.ignore 
						=
						 
						true
						;
   
						this
						.ignore 
						=
						 
						true
						;
						17
						
								 else
						 
						if
						 (value.equalsIgnoreCase(
						"
						true
						"
						))
  
						else
						 
						if
						 (value.equalsIgnoreCase(
						"
						true
						"
						))
						18
						
								 this
						.ignore 
						=
						 
						true
						;
   
						this
						.ignore 
						=
						 
						true
						;
						19
						
								 else
						 
						if
						 (value.equalsIgnoreCase(
						"
						yes
						"
						))
  
						else
						 
						if
						 (value.equalsIgnoreCase(
						"
						yes
						"
						))
						20
						
								 this
						.ignore 
						=
						 
						true
						;
   
						this
						.ignore 
						=
						 
						true
						;
						21
						
								 else
  
						else
						
								
						
						22
						
								 this
						.ignore 
						=
						 
						false
						;
   
						this
						.ignore 
						=
						 
						false
						;
						23
						
								 }
 }
				
				
						
				
				24
				
						 
				
		 
		 
		    3.Struts-config.xml:
 设置资源文件的路径
 
		
				1
				 <
				message
				-
				resources
				<
				message
				-
				resources
				2
				
						 parameter
				=
				"
				cn.redstoneinfo.oss.security.web.security-resource
				"
				 key
				=
				"
				SECURITY_RES
				"
				 
				null
				=
				"
				false
				"
				/>
  parameter
				=
				"
				cn.redstoneinfo.oss.security.web.security-resource
				"
				 key
				=
				"
				SECURITY_RES
				"
				 
				null
				=
				"
				false
				"
				/>
				
						
				
				3
				
						 
  
				4
				
						 <
				message
				-
				resources
 
				<
				message
				-
				resources
				5
				
						 parameter
				=
				"
				cn.redstoneinfo.oss.web.ApplicationResources
				"
				 
				null
				=
				"
				false
				"
				/>
  parameter
				=
				"
				cn.redstoneinfo.oss.web.ApplicationResources
				"
				 
				null
				=
				"
				false
				"
				/>
				
						
				
				6
				
						 
				
		 
		    4.role-list.jsp
 页面编码和调用资源
 
		
				 1
				 <%
				@ page contentType
				=
				"
				text/html; charset=UTF-8
				"
				 language
				=
				"
				java
				"
				%>
				<%
				@ page contentType
				=
				"
				text/html; charset=UTF-8
				"
				 language
				=
				"
				java
				"
				%>
				 
				 2
				
						 
        

				 3
				
						 <
				oss:panel label
				=
				"
				security.role.list.title
				"
				 bundle
				=
				"
				SECURITY_RES
				"
				>
 
				<
				oss:panel label
				=
				"
				security.role.list.title
				"
				 bundle
				=
				"
				SECURITY_RES
				"
				>
				
						
				
				 4
				
						 <
				table width
				=
				"
				100%
				"
				>
 
				<
				table width
				=
				"
				100%
				"
				>
				
						
				
				 5
				
						 <
				tr 
				class
				=
				"
				tr1
				"
				 align
				=
				"
				center
				"
				 height
				=
				"
				25
				"
				>
 
				<
				tr 
				class
				=
				"
				tr1
				"
				 align
				=
				"
				center
				"
				 height
				=
				"
				25
				"
				>
				
						
				
				 6
				
						 <
				td width
				=
				"
				5%
				"
				>
     
				<
				td width
				=
				"
				5%
				"
				>
				
						
				
				 7
				
						 </
				td
				>
     
				</
				td
				>
				
						
				
				 8
				
						 <
				td 
				>
     
				<
				td 
				>
				  
				 9
				
						 <
				bean:message key
				=
				"
				app.common.name
				"
				 
				/>
  
				<
				bean:message key
				=
				"
				app.common.name
				"
				 
				/>
				
						
				
				10
				
						 </
				td
				>
     
				</
				td
				>
				
						
				
				11
				
						 <
				td 
				>
     
				<
				td 
				>
				
						
				
				12
				
						 <
				bean:message key
				=
				"
				app.common.activeDate
				"
				 
				/>
     
				<
				bean:message key
				=
				"
				app.common.activeDate
				"
				 
				/>
				
						
				
				13
				
						 </
				td
				>
     
				</
				td
				>
				
						
				
				14
				
						 <
				td
				>
     
				<
				td
				>
				
						
				
				15
				
						 <
				bean:message key
				=
				"
				app.common.inactiveDate
				"
				 
				/>
     
				<
				bean:message key
				=
				"
				app.common.inactiveDate
				"
				 
				/>
				
						
				
				16
				
						 </
				td 
				>
     
				</
				td 
				>
				
						
				
				17
				
						 <
				td
				>
     
				<
				td
				>
				
						
				
				18
				
						 <
				bean:message key
				=
				"
				app.common.status
				"
				 
				/>
     
				<
				bean:message key
				=
				"
				app.common.status
				"
				 
				/>
				
						
				
				19
				
						 </
				td
				>
     
				</
				td
				>
				   
				20
				
						 
    
				21
				
						 </
				tr
				>
 
				</
				tr
				>
				
						
				
				22
				
						 
        

				23
				
						 <
				input type
				=
				"
				button
				"
				 value
				=
				"
				   <bean:message key=
				"
				app.common.
				new
				"
				/>
				"
				 
				class
				=
				"
				buttonnew
				"
				 onclick
				=
				"
				javascript:document.forms[0].act.value='add'; goUrlFormTarget('role.do', 'roleForm', 'roleTreeFrame');
				"
				>
        
				<
				input type
				=
				"
				button
				"
				 value
				=
				"
				   <bean:message key=
				"
				app.common.
				new
				"
				/>
				"
				 
				class
				=
				"
				buttonnew
				"
				 onclick
				=
				"
				javascript:document.forms[0].act.value='add'; goUrlFormTarget('role.do', 'roleForm', 'roleTreeFrame');
				"
				>
				    
				24
				
						 
				
		 
		
				
    5.ApplicationResources.properties
 
		
				1
				 app.common.name 
				=
				 名字
				app.common.name 
				=
				 名字
				2
				
						 app.common.ip 
				=
				 IP
 app.common.ip 
				=
				 IP
				3
				
						 app.common.port 
				=
				 端口
 app.common.port 
				=
				 端口
				4
				
						 app.common.status 
				=
				 状态
 app.common.status 
				=
				 状态 
				5
				
						 
				
		 
		    6.build.xml
 编译资源文件生成Unicode
		
				1
				 <
				native2ascii src
				=
				"
				${oss.src.dir}
				"
				 encoding
				=
				"
				UTF-8
				"
				 dest
				=
				"
				${target.dir}/WEB-INF/classes
				"
				>
				 
				<
				native2ascii src
				=
				"
				${oss.src.dir}
				"
				 encoding
				=
				"
				UTF-8
				"
				 dest
				=
				"
				${target.dir}/WEB-INF/classes
				"
				>
				
						
				
				2
				
						 <
				include name
				=
				"
				**/*.properties
				"
				 
				/>
   
				<
				include name
				=
				"
				**/*.properties
				"
				 
				/>
				
						
				
				3
				
						 </
				native2ascii
				>
  
				</
				native2ascii
				>
				  
				4
				
						 
				
		 
		    7.转换后的ApplicationResources.properties
 
		
				1
				 app.common.name 
				=
				 \u540d\u5b57
				app.common.name 
				=
				 \u540d\u5b57
				2
				
						 app.common.ip 
				=
				 IP
 app.common.ip 
				=
				 IP
				3
				
						 app.common.port 
				=
				 \u7aef\u53e3
 app.common.port 
				=
				 \u7aef\u53e3
				4
				
						 app.common.status 
				=
				 \u72b6\u6001
 app.common.status 
				=
				 \u72b6\u6001
				5
				
						 
				
		 
		    8.server.xml
 tomcat5.0以后post跟get提交方法不了不同的编码处理,应该用下面的方式来处理
     
		
				1
				 <
				Connector
				<
				Connector
				2
				
						 port
				=
				"
				8080
				"
				               maxHttpHeaderSize
				=
				"
				8192
				"
  port
				=
				"
				8080
				"
				               maxHttpHeaderSize
				=
				"
				8192
				"
				
						
				
				3
				
						 maxThreads
				=
				"
				150
				"
				 minSpareThreads
				=
				"
				25
				"
				 maxSpareThreads
				=
				"
				75
				"
               maxThreads
				=
				"
				150
				"
				 minSpareThreads
				=
				"
				25
				"
				 maxSpareThreads
				=
				"
				75
				"
				
						
				
				4
				
						 enableLookups
				=
				"
				false
				"
				 redirectPort
				=
				"
				8443
				"
				 acceptCount
				=
				"
				100
				"
               enableLookups
				=
				"
				false
				"
				 redirectPort
				=
				"
				8443
				"
				 acceptCount
				=
				"
				100
				"
				
						
				
				5
				
						 connectionTimeout
				=
				"
				20000
				"
				 disableUploadTimeout
				=
				"
				true
				"
				  URIEncoding
				=
				"
				UTF-8
				"
				/>
               connectionTimeout
				=
				"
				20000
				"
				 disableUploadTimeout
				=
				"
				true
				"
				  URIEncoding
				=
				"
				UTF-8
				"
				/>
				
						
				
				6
				
						 
				
		 
		    9.界面

    以上操作基本实现彻底消灭中文乱码问题,当然还有其他更好的办法和应用,我们这里只讲struts的简单应用中如何避免相应问题的处理办法。