JAVA中中文亂碼問題

一,html

    Java源文件中極可能包含中文,而Java和JSP源文件保存的方式是基於字節流的。若是Java或JSP編譯成class文件過程當中,使用的編碼方式和源碼方式不一致,則會出現亂碼。在HTML頁面中,因爲頁面編碼和瀏覽器的編碼方式不一樣,也會出現亂碼。
java

    1,結果HTML中中文亂碼問題
mysql

    在每一個HTML頁面中的<head></head>標籤中增長
web

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

    2,解決jsp中中文亂碼問題
spring

    pageEncoding是jsp文件自己的編碼,contextType的charset是服務器傳送給客戶端的內容編碼
sql

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

    3,解決頁面數據傳輸亂碼問題
數據庫

    採用編碼過濾器來解決,設置一個過濾器,把全部的編碼都統一瀏覽器

<!--定義編碼過濾器-->
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacerEncodingFilter</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>

       4,HTTP(post)中出現亂碼
服務器

        在servlet頁面中添加
app

response.setCharacterEncoding("utf-8") ;

    5,HTTP(get)中出現亂碼

    若是是get,須要獲取請求的字符串,將字符串進行轉換,同時須要知道源碼

//獲取請求的字符串
String str = resquest.getParameter("param") ;
str = new String(str.getBytes("ISO-8859-1"),"UTF-8") ;

    6,MySql數據庫中中文問題

    解決MySql數據庫中的中文問題,主要在JDBC的驅動url中添加

jdbc:mysql://localhost:3306/test?user=root&password=123&useUnicode=true&characterEncoding=UTF-8
相關文章
相關標籤/搜索