我的Blog我做主^_^

走向一条通往JAVA的不归路...

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  64 随笔 :: 68 文章 :: 77 评论 :: 0 Trackbacks

有人说,乱码问题一直跟中国的程序员特别有缘,真是再同意不过了,不管是Struts,JSF,JSP,还是MySQL,Tomcat,全都或多或少有乱码的问题。
一般的做法有用Filter:

 < filter >
     < filter-name > Set Character Encoding </ filter-name >
     < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
     < init-param >
       < param-name > encoding </ param-name >
       < param-value > GBK </ param-value >
     </ init-param >
     < init-param >
       < param-name > ignore </ param-name >
       < param-value > true </ param-value >
     </ init-param >
   </ filter >
   < filter-mapping >
     < filter-name > Set Character Encoding </ filter-name >
     < url-pattern > *.do </ url-pattern >
   </ filter-mapping >
   < filter-mapping >
     < filter-name > Set Character Encoding </ filter-name >
     < url-pattern > *.jsp </ url-pattern >
   </ filter-mapping >
   < filter-mapping >
     < filter-name > Set Character Encoding </ filter-name >
     < url-pattern > *.html </ url-pattern >
   </ filter-mapping >
   < filter-mapping >
     < filter-name > Set Character Encoding </ filter-name >
     < url-pattern > *.htm </ url-pattern >
   </ filter-mapping >

的,有用


 <% request.setCharacterEncoding( " GBK " ); %>
的,还有用


 <% @ page contentType = " text/html; charset=GBK "  pageEncoding = " GBK " %>

<meta http-equiv="content-type" content="text/html; charset=GBK">

的,还可以用


 <%  String name  =   new  String(request.getParameter( " name " ).getBytes( " 8859_1 " ),  " GB2312 " );  %>

昨天就在做项目的过程中,发现用URL传request参数的时候,在第二个页面上得到乱码的问题。把上面几种方法都试了一下还是不行。仔细追踪了一下,发现在页面的源代码上中文是正常的,一直到URL还是中文正常,可是在后台的Action里面log出来就成了乱码了,于是猜想是在request封装的过程中把中文变成乱码了,以致于后台直接就是取到的乱码。在后台Action中Set入中文,页面上正常显示,说明Struts的中文已经不存在问题。剩下的,应该就只有doGet和doPost方法的问题了。找了一下tomcat的配置文件,发现只要在server.xml中:


      <!--  Define a non-SSL HTTP/1.1 Connector on port 8080  -->
     < Connector  port ="8080"  maxHttpHeaderSize ="8192"
               maxThreads ="150"  minSpareThreads ="25"  maxSpareThreads ="75"
               enableLookups ="false"  redirectPort ="8443"  acceptCount ="100"
               connectionTimeout ="20000"  disableUploadTimeout ="true" />
     <!--  Note : To disable connection timeouts, set connectionTimeout value
     to 0  -->
改为


      <!--  Define a non-SSL HTTP/1.1 Connector on port 8080  -->
     < Connector  port ="8080"  maxHttpHeaderSize ="8192"
               maxThreads ="150"  minSpareThreads ="25"  maxSpareThreads ="75"
               enableLookups ="false"  redirectPort ="8443"  acceptCount ="100"
               connectionTimeout ="20000"  disableUploadTimeout ="true"  URIEncoding ="GBK"/>
     <!--  Note : To disable connection timeouts, set connectionTimeout value
     to 0  -->
就是加上URIEncoding="GBK"就万事大吉了。

再加上一条:

<session-factory>
  <property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.0.3:1433;DatabaseName=HomeConsume;charset=GBK</property>
  <property name="hibernate.cglib.use_reflection_optimizer">true</property>
  <property name="hibernate.connection.password">sju</property>
  <property name="hibernate.connection.username">sa</property>
  <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
  <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
  <mapping resource="net/magicyang/homeconsume/pojo/Test.hbm.xml" />
  <mapping resource="net/magicyang/homeconsume/pojo/Consumeinfo.hbm.xml" />
  <mapping resource="net/magicyang/homeconsume/pojo/Consumetype.hbm.xml" />
  </session-factory>
至此,应该再困难的乱码问题都解决了吧。就是要在页面上、数据库中、request里、doGet、doPost方法里面都是中文!看你还有什么地方躲??



posted on 2007-01-24 10:00 java_蝈蝈 阅读(250) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: