JAVA 亂碼問題處理總結

來源:互聯網
上載者:User

標籤:

中文亂碼問題及其解決方案


1.解決HTML頁面中的中文問題:為了使HTML頁面很好地支援中文,就必須在每個HTML頁面的頭部增加如下代碼:

<HEAD>

...

<META http-equiv=Content-Type content="text/html;charset=utf-8">

...

<HEAD>

2.解決JSP頁面中的中文問題為了使JSP頁面很好地支援中文,就必須在每個JSP頁面的頭部增加如下代碼:

<%@ page contentType="text/html;charset=utf-8" language="java"%>

3.解決Servlet響應結果的中文問題為了使Servlet頁面很好地支援中文,就必須在每個Servlet頁面的頭部增加如下代碼:

request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

4.解決頁面資料轉送的中文問題為了使中文資料在各頁面(組件)之間正常傳遞,最佳的方法就是採用編碼過濾器來解決。在WEB.XML中配置一個編碼過濾器,內容如下:

<!--定義編碼過濾器-->

    <filter>
        <filter-name>encodingFilter</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>  

    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

5.解決HTTP(get)請求中的中文問題:在預設情況下,IE瀏覽器發“ISO-8859-1”的編碼格式發送請求,如果接收到HTTP的get請求中文參數時出現亂碼,就可以對其進行編碼轉換,例如:

String param = request.getParameter("param");

param = new String(param.getBytes("ISO-8859-1", "utf-8"));

也可以通過修改Tomcat的server.xml檔案來解決:

<Connector port = "8080"

...

URIEncoding="utf-8"/> -->增加這項

6.解決MySQL資料庫的中文問題:解決MySQL SQL SERVER資料庫中文問題主要在JDBC驅動的URL上,例如:

jdbc:mysql://localhost/test?user=root&password=123456&useUnicode=true&characterEncoding=utf8

"jdbc:microsoft:sqlserver://hzz:1433;useUnicode=true&characterEncoding=gb2312;databaseName=Heros","sa","13871955777"

 

JAVA 亂碼問題處理總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.