Tomcat解决中文乱码之道(GET/POST)

1、GET方法:

       问题

     输入URL地址:Http://localhost:8080/test/my.jsp?name=中国

     后台接收name值的时候是乱码。

     

     原因: 

      tomcat对get方式提交的请求对query-string   处理时采用了和post方法不一样的处理方式。(与tomcat4不一样,所以设置setCharacterEncoding("GBK"))不起作用的


       解决方案:

        1)  打开tomcat的server.xml文件,找到区块,加入如下一行:   

          URIEncoding=”GBK”   <GBK取决于浏览器对URL的编码方式>

          完整的配置应如下:   

               <Connector    port="80" maxThreads="150"   minSpareThreads="25"                                                               maxSpareThreads="75"      enableLookups="false"   redirectPort="8443"                                                               acceptCount="100"   debug="0"   connectionTimeout="20000"     

                                                             disableUploadTimeout="true"   URIEncoding="GBK"   />   

       2)重启tomcat,一切OK。

2、POST方法:

      问题:

     表单提交的数据,用request.getParameter("xxx")返回的字符串为乱码或者??


     原因:

     tomcat的j2ee实现对表单提交即post方式提示时处理参数采用缺省的iso-8859-1来处理

       

     解决方案:

     1)   实现一个Filter.设置处理字符集为GBK。(在tomcat的webapps/servlet-examples目录有一个完整的例子。请参考web.xml和SetCharacterEncodingFilter的配置。)

        

     2)在你的web.xml里加入如下几行:   

          <filter>   

                  <filter-name>SetCharacterEncoding</filter-name>   

                  <filter-class>filters.SetCharacterEncodingFilter</filter-class>   

                  <init-param>   

                          <param-name>encoding</param-name>   

                          <param-value>GBK</param-value>   

                  </init-param>   

          </filter>   

          <filter-mapping>   

                  <filter-name>SetCharacterEncoding</filter-name>   

                  <url-pattern>/*</url-pattern>   

          </filter-mapping>   

      3)完成. 

  已经验证

猜你喜欢

转载自wkm.iteye.com/blog/1747311