程式中文亂碼問題解決大全

來源:互聯網
上載者:User

本人在學習Java的這段時間裡,尤其是學習到J2EE這塊內容時,經常遇到的一個問題就是中文亂碼問題,特別的頭疼,相信大家也會有同感,故在學習的時候不斷的總結,發現解決這類問題無非是如下幾點(以gb2312為例),和大家分享一下心得:

1、TOMCAT中解決中文亂碼

在conf/server.xml檔案中,增加一句URIEncoding="gb2312"的代碼到下面的地方,選擇合適的編碼方式

  <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" 

redirectPort="8443" URIEncoding="gb2312"/>  

2、servlet中解決中文亂碼

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

response.setCharacterEncoding("gb2312");
request.setCharacterEncoding("gb2312");

3、串連mysql資料庫解決中文亂碼

String url = "jdbc:mysql://localhost:3306/DBName?characterEncoding=gb2312";

4、HTML頁面解決中文亂碼

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

5、JSP頁面解決中文亂碼

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

6、mysql資料庫解決中文亂碼
簡單有效一個解決方案:
開啟\MySQL Server 5.0\my.ini
找到
[mysql]
default-character-set=latin1

# created and no character set is defined
default-character-set=latin1

改成
[mysql]
default-character-set=gb2312
# created and no character set is defined
default-character-set=gb2312

以上就是我目前所學到的知識中出現過的問題,相信還有更多的解決方案以及新的問題,所以也希望和大家一塊來討論一下,總結所有的中文亂碼問題!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.