mysql解決中文亂碼,過濾器解決中文碼

來源:互聯網
上載者:User

一 mysql設定字元集(有四處)
環境(選 Best Support For Multilingualism 即UTF8),
庫,表,欄位
規則如下:
庫沒有特別指定,就使用環境設定的字元集.
表沒有特別指定,就使用庫設定的字元集.
欄位沒有特別指定,就使用表設定的字元集.

一般在建表時都指定表的字元集.最好是把MYSQL環境,庫,表設定成一樣的字元集.會
減少不必要的麻煩.

MySQL資料庫設定
a)windows系統: windows下安裝mysql時,編碼選擇utf-8,查看mysql安裝目錄下my,ini檔案,在[mysqld]段下面加入default-character-set=utf8

二 解決中文亂碼
url中加上 "useUnicode=true&characterEncoding=utf8"

new String(request.getParameter("test").getBytes("iso-8859-1"),"GBK")

但這樣的代碼相信不是一個解決的辦法,這樣會增加程式的複雜度,
,終於找到了完美的解決方式,在TOMCAT中只需要簡單的配置,引入2個檔案就可以
輕鬆搞定。
三 使用過濾器解決中文亂碼
在TOMCAT中  webapps/jsp-examples/WEB_INF/classes/filter目錄中
找到這2個檔案RequestDumperFilter.java,SetCharacterEncodingFilter.java,
複製到filters包下

步驟2:配置WEB.XML

在web.xml裡加入這一段(記住在加在display-name項和servlet項之間,因為web.xml
檔案頭設定的DTD檔案限定了各項的順序)

……

<filter>

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

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

<init-param>

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

<param-value>utf8</param-value>

</init-param>

</filter>

<filter-mapping>

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

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

</filter-mapping>

……
以後寫jsp就像寫書本一樣,加上
<%@ page contentType="text/html";charset=utf8"%>
<META http-equiv=Content-Type content="text/html";charset=utf8">

四第一原則:所有編碼的地方,統一設為UTF-8,這樣繁體,簡體,日文,韓文。。。通吃了)
一、下載中文檔案名稱檔案問題
a)Tomcat:修改Tomcat安裝目錄下conf/server.xml檔案,加上URIEncoding="UTF-8"/
<Connector port="8080" .... URIEncoding="UTF-8"/>

b)JBOSS :安裝目錄下 server/default/deploy/jbossweb-tomcat55.sar/server.xml
<Connector port="8080" .... URIEncoding="UTF-8"/
 

聯繫我們

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