mysql,Struts Servlet 和 JSP 中解決中文問題

來源:互聯網
上載者:User

有多種

(1)在Mysql目錄下找到my.ini
開啟,找到default-character-set設定,把它改成:
default-character-set=gb2312

如果是5.0或更高版本,可以在配置面板裡設定:
詳見:http://ycoe.cnblogs.com/articles/354485.html

(2)String newName = new String(name.getBytes("iso-8859-1"),"gbk");

1、確定你在安 Mysql時的字元集是 gbk
------>1、 <%@ page language="java" pageEncoding="UTF-8"%>
------>2、 如果你在JSP裡有中文,好那可以正常顯示,加上一個struts標籤的form 也有正常的顯示為中文,但是當你在表單 裡提交中文的時候就會變成亂碼
------>3、 變成亂碼以後在與資料庫的資料進行比較的時候就會出錯,而且倒回表單時你會看到?????的亂碼
------>4、 解決辦法是 把 <%@ page language="java" pageEncoding="UTF-8"%> 刪除掉 在第一行加上
<%@ page contentType="text/html;charset=gb2312"%>
和在
<head><meta http-equiv="Content-Type" content="text/html; charset=GB2312" /></head>
(注意一定在head裡第一行)
------>5、當用post提交資料時,也會變成亂碼,解決辦法是

把專案檔下的 WEB-INF/web.xml
加上以下代碼
<filter>
<filter-name>Set Character Encoding</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>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

然後把servlet裡的WEB-INF/classes/filter目錄拷貝到項目裡對應的目錄,這樣就可以解決因post提交引起的中文問題
OK!改成STRUTS中文問題

servlet的中文問題

response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("GB2312");

struts 中文問題
把資源檔用native2ascii.exe 編譯一下,放在原來的位置就可以啦

聯繫我們

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