解決jsp,tomcat,MYSQL下中文亂碼問題

查看tomcat服務器標簽專題

        這些天開發一個項目,服務器是tomcat,操作系統是xp,采用的是MVC架構,模式是采用Facade模式,總是出現亂碼,通過簡單的設置頁面字符集,總算可以正確顯示中文,可是沒想到表單里提交的數據里的中文還是有亂碼,我狂暈,沒想到JSP里的亂碼問題比ASP里嚴重多了,自己也解決了好多天,同事也幫忙解決,也參考了網上眾多網友的文章和意見,總算是搞定。但是好記性不如爛筆桿,所以特意記下,以防止自己遺忘,同時也給那些遇到同樣問題的人提供一個好的參考途徑:

        以下內容參考了網上的方法

(一)    JSP設計頁面上是中文,但運行時看到的是亂碼:

解決的辦法就是在JSP頁面的編碼的地方<%@ page language="java" contentType="text/html;charset=GBK" %>,因為Jsp轉成Java文件時的編碼問題,默認的話有的服務器是ISO-8859-1,如果一個JSP中直接輸入了中文,Jsp把它當作ISO8859-1來處理是肯定有問題的,這一點,我們可以通過查看Jasper所生成的Java中間文件來確認

(二)    當用Request對象獲取客戶提交的漢字代碼的時候,會出現亂碼,比如表單里:

解決的辦法是:要配置一個filter,也就是一個Servelet的過濾器,代碼如下:

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.UnavailableException;

public class SetCharacterEncodingFilter implements Filter {

    public void destroy() 

     {

    }

    public void doFilter(ServletRequest request, ServletResponse response,

                                  FilterChain chain)throws IOException, ServletException 

     {

    request.setCharacterEncoding("GBK");

    // 傳遞控制到下一個過濾器

    chain.doFilter(request, response);

    }

    public void init(FilterConfig filterConfig) throws ServletException 

     {

    }

}

配置web.xml

<filter>

<filter-name>Set Character Encoding</filter-name>

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

</filter>

<filter-mapping>

<filter-name>Set Character Encoding</filter-name>

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

</filter-mapping>

如果你的還是出現這種情況的話你就往下看看是不是你出現了第四中情況,你的Form提交的數據是不是用get提交的,一般來說用post提交的話是沒有問題的,如果是的話,你就看看第四中解決的辦法。

還有就是對含有漢字字符的信息進行處理,處理的代碼是:

package dbJavaBean;

public class CodingConvert

{   

public CodingConvert()

{

         //process

}

public String toGb(String uniStr)

{

     String gbStr = "";

     if(uniStr == null)

       {

             uniStr = "";

     }

     try

       {

   byte[] tempByte = uniStr.getBytes("ISO8859_1");

   gbStr = new String(tempByte,"GB2312");

     }

      catch(Exception ex)

      {

              // exception process

    }

     return gbStr;

 }

 public String toUni(String gbStr)

 {

     String uniStr = "";

     if(gbStr == null)

       {

            gbStr = "";

     }

     try

       {

   byte[] tempByte = gbStr.getBytes("GB2312");

   uniStr = new String(tempByte,"ISO8859_1");

     }

       catch(Exception ex)

      {

    }

     return uniStr;

}

}

你也可以在直接的轉換,首先你將獲取的字符串用ISO-8859-1進行編碼,然后將這個編碼存放到一個字節數組中,然后將這個數組轉化成字符串對象就可以了,例如:

String str=request.getParameter(“girl”);

Byte B[]=str.getBytes(“ISO-8859-1”);

Str=new String(B);

通過上述轉換的話,提交的任何信息都能正確的顯示。

(三)    在Formget請求在服務端用request. getParameter(“name”)時返回的是亂碼;按tomcat的做法設置Filter也沒有用或者用request.setCharacterEncoding("GBK");也不管用問題是出在處理參數傳遞的方法上:如果在servlet中用doGet(HttpServletRequest request, HttpServletResponse response)方法進行處理的話前面即使是寫了:

request.setCharacterEncoding("GBK");

response.setContentType("text/html;charset=GBK");

也是不起作用的,返回的中文還是亂碼!!!如果把這個函數改成doPost(HttpServletRequest request, HttpServletResponse response)一切就OK了。

同樣,在用兩個JSP頁面處理表單輸入之所以能顯示中文是因為用的是post方法傳遞的,改成get方法依舊不行。

由此可見在servlet中用doGet()方法或是在JSP中用get方法進行處理要注意。這畢竟涉及到要通過瀏覽器傳遞參數信息,很有可能引起常用字符集的沖突或是不匹配。

解決的辦法是:

1) 打開tomcat的server.xml文件,找到區塊,加入如下一行: 

URIEncoding=”GBK” 

完整的應如下: 

<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

 enableLookups="false" redirectPort="8443" acceptCount="100" debug="0"

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

2)重啟tomcat,一切OK。

需要加入的原因大家可以去研究 $TOMCAT_HOME/webapps/tomcat-docs/config/http.html下的這個文件就可以知道原因了。需要注意的是:這個地方如果你要是用UTF-8的時候在傳遞的過程中在Tomcat中也是要出現亂碼的情況,如果不行的話就換別的字符集。

(四)    JSP頁面上有中文,按鈕上面也有中文,但是通過服務器查看頁面的時候出現亂碼:

     解決的辦法是:首先在JSP文件中不應該直接包含本地化的消息文本,而是應該通過<bean:message>標簽從Resource Bundle中獲得文本。應該把你的中文文本放到Application.properties文件中,這個文件放在WEB-INF/classes/*下,例如我在頁面里有姓名,年齡兩個label,我首先就是要建一個Application.properties,里面的內容應該是name=”姓名” age=”年齡”,然后我把這個文件放到WEB-INF/classes/properties/下,接下來根據Application.properties文件,對他進行編碼轉化,創建一個中文資源文件,假定名字是Application_cn.properties。在JDK中提供了native2ascii命令,他能夠實現字符編碼的轉換。在DOS環境中找到你放置Application.properties的這個文件的目錄,在DOS環境中執行一下命令,將生成按GBK編碼的中文資源文件Application_cn.properties:native2ascii ?encoding gbk Application.properties Application_cn.properties執行以上命令以后將生成如下內容的Application_cn.properties文件:name="u59d3"u540d age="u5e74"u9f84,在Struts-config.xml中配置:<message-resources parameter="properties.Application_cn"/>。到這一步,基本上完成了一大半,接著你就要在JSP頁面上寫<%@ page language="java" contentType="text/html;charset=GBK" %>,到名字的那個label是要寫<bean:message key=”name”>,這樣的化在頁面上出現的時候就會出現中文的姓名,年齡這個也是一樣,按鈕上漢字的處理也是同樣的。

(五)    寫入到數據庫是亂碼:

解決的方法:要配置一個filter,也就是一個Servelet的過濾器,代碼如同第二種時候一樣。

如果你是通過JDBC直接鏈接數據庫的時候,配置的代碼如下:jdbc:mysql://localhost:3306/workshopdb?useUnicode=true&characterEncoding=GBK,這樣保證到數據庫中的代碼是不是亂碼。

如果你是通過數據源鏈接的化你不能按照這樣的寫法了,首先你就要寫在配置文件中,在tomcat 5.0.19中配置數據源的地方是在

C:"Tomcat 5.0"conf"Catalina"localhost這個下面,我建立的工程是workshop,放置的目錄是webapp下面,workshop.xml的配置文件如下:

<!-- insert this Context element into server.xml -->

<Context path="/workshop" docBase="workshop" debug="0"

           reloadable="true" >

  <Resource name="jdbc/WorkshopDB"

               auth="Container"

               type="javax.sql.DataSource" />

  <ResourceParams name="jdbc/WorkshopDB">

    <parameter>

      <name>factory</name>

      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

    </parameter>

    <parameter>

      <name>maxActive</name>

      <value>100</value>

    </parameter>

    <parameter>

      <name>maxIdle</name>

      <value>30</value>

    </parameter>

    <parameter>

      <name>maxWait</name>

      <value>10000</value>

    </parameter>

      <parameter>

     <name>username</name>

     <value>root</value>

    </parameter>

    <parameter>

     <name>password</name>

     <value></value>

    </parameter>

    <!-- Class name for mm.mysql JDBC driver -->

    <parameter>

       <name>driverClassName</name>

       <value>com.mysql.jdbc.Driver</value>

     </parameter>

   <parameter>

      <name>url</name>

        <value>

           <![CDATA[jdbc:mysql://localhost:3306/workshopdb?useUnicode=true&amp;characterEncoding=GBK]]>

        </value>

    </parameter>

  </ResourceParams>

</Context>

粗體的地方要特別的注意,和JDBC直接鏈接的時候是有區別的,如果你是配置正確的化,當你輸入中文的時候到數據庫中就是中文了,有一點要注意的是你在顯示數據的頁面也是要用<%@ page language="java" contentType="text/html;charset=GBK" %>這行代碼的。需要注意的是有的前臺的人員在寫代碼的是后用Dreamver寫的,寫了一個Form的時候把他改成了一個jsp,這樣有一個地方要注意了,那就是在Dreamver中Action的提交方式是request的,你需要把他該過來,因為在jsp的提交的過程中緊緊就是POST和GET兩種方式,但是這兩種方式提交的代碼在編碼方面還是有很大不同的,這個在后面的地方進行說明。3

以上就是我在開發系統中解決中文的問題,不知道能不能解決大家的問題,時間匆忙,沒有及時完善,文筆也不是很好,有些地方估計是詞不達意。大家可以給我意見,希望能共同進步。

其它上按以上的方法就可以解決的。

第(二)種方法里,這個過濾器比較簡單,如果字符集不同的話,那就要手動修改這個過濾器,下面介紹一個功能強的過濾器:


package com.manage.filter;

import javax.servlet.*;
import java.io.IOException;


public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {

this.encoding = null;
this.filterConfig = null;

}


public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);

}


public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;

}
protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}//EOC

/**在web.xml里這樣設置
  <filter>
  <filter-name>Set Character Encoding</filter-name>
  <filter-class>
   com.manage.filter.SetCharacterEncodingFilter
  </filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</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>
  <servlet-name>action</servlet-name>
 </filter-mapping>
*/

針對第(二)種方法,還有一個很簡單的方法,就是在每個頁面里都加上以下代碼:<%request.setCharacterEncoding("gb2312");%>
<%response.setCharacterEncoding("gb2312");%>
這樣聽說行,不過我試了沒有效果

        針對MYSQL數據庫的中文亂碼問題,我已經總結了一篇專門的解決方法,

請參考http://www.busfly.cn/post/58.html 再談亂碼問題,如何解決MYSQL數據中文亂碼問題