JSP頁面顯示亂碼解決方法

轉載於:http://blog.csdn.net/yzh54ak/article/details/5880616

一、JSP頁面顯示亂碼

下面的顯示頁面(display.jsp)就出現亂碼:

<html>

<head>

<title>JSP的中文處理</title>

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

</head>

 

<body>

<%

out.print("JSP的中文處理");

%>

</body>

</html>

對不同的WEB服務器和不同的JDK版本,處理結果就不一樣。原因:服務器使用的編碼方式不同和瀏覽器

對不同的字符顯示結果不同而導致的。解決辦法:在JSP頁面中指定編碼方式(gb2312),即在頁面的第一行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除亂碼了。完整頁面如下

<%@ page contentType="text/html; charset=gb2312"%>

<html>

<head>

<title>JSP的中文處理</title>

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

</head>

<body>

<%

out.print("JSP的中文處理");

%>

</body>

</html>

 

二、表單提交中文時出現亂碼

下面是一個提交頁面(submit.jsp),代碼如下:

<html>

<head>

<title>JSP的中文處理</title>

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

</head>

 

<body>

<form name="form1" method="post" action="process.jsp">

<div align="center">

<input type="text" name="name">

<input type="submit" name="Submit" value="Submit">

</div>

</form>

</body>

</html>

下面是處理頁面(process.jsp)代碼:

<%@ page contentType="text/html; charset=gb2312"%>

<html>

<head>

<title>JSP的中文處理</title>

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

</head>

<body>

<%=request.getParameter("name")%>

</body>

</html>

如果submit.jsp提交英文字符能正確顯示,如果提交中文時就會出現亂碼。原因:瀏覽器默認使用UTF-8編碼方式來發送請求,而UTF- 8和GB2312編碼方式表示字符時不一樣,這樣就出現了不能識別字符。

解決辦法:通過request.setCharacterEncoding ("gb2312")對請求進行統一編碼,就實現了中文的正常顯示。修改後的process.jsp代碼如下:

<%@ page contentType="text/html; charset=gb2312"%>

<%

request.setCharacterEncoding("gb2312");

%>

<html>

<head>

<title>JSP的中文處理</title>

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

</head>

<body>

<%=request.getParameter("name")%>

</body>

</html>

 

三、數據庫連接出現亂碼

只要涉及中文的地方全部是亂碼,解決辦法:在數據庫的數據庫URL中加上

useUnicode=true&characterEncoding=GBK 就OK了。

 

四、數據庫的顯示亂碼

在mysql4.1.0中,varchar類型,text類型就會出現中文亂碼,對於varchar類型把它設爲binary屬性就可以解決中文問題,對於text類型就要用一個編碼轉換類來處理,實現如下:

public class Convert {

/** 把ISO-8859-1碼轉換成GB2312

*/

public static String ISOtoGB(String iso){

String gb;

try{

if(iso.equals("") || iso == null){

return "";

}

else{

iso = iso.trim();

gb = new String(iso.getBytes("ISO-8859-1"),"GB2312");

return gb;

}

}

catch(Exception e){

System.err.print("編碼轉換錯誤:"+e.getMessage());

return "";

}

}

}

把它編譯成class,就可以調用Convert類的靜態方法ISOtoGB()來轉換編碼。

總結:

1.    在jsp中<%@ page contentType="text/html; charset=A" %>如果指定了,那麼在改jsp中所有構造的String(不是引用),如果沒有指定編碼,那麼這些String的編碼是A的。從request的得到的String如果沒有指定request的編碼的話,他是iso-8859-1的。從別的地方得到的String是使用原來初始的編碼的,比如從數據庫得到String,如果數據庫的編碼是B,那麼該String的編碼是B而不是A的,也不是系統默認的。 此時,如果要輸出的String的編碼不是A,那麼,很可能顯示亂碼的,所以首先要將String正確轉化爲編碼A的String,然後輸出。

2.    在jsp中<%@ page contentType="text/html; charset=A" %>沒有指定,那麼相當於指定了<%@ page contentType="text/html; charset=ISO-8859-1" %>

 

3. Servelte中如果執行了像 response.setContentType("text/html;charset=A");説明將response的字符輸出流編碼設置爲A,所有要輸出的String的編碼要轉化爲A的,否則會得到亂碼的。

      Servelet中從request得到的String的編碼和jsp中一樣的,但是在servlet java文件中構造的String是使用的系統默認的編碼的。在servelt中從外部得到的String 是使用原來的編碼的,比如從編碼爲B的數據庫得到的數據是編碼爲B的,不是A,也不是系統默認的編碼。

 

 

五、Linux 下java中文環境設置

首先,檢查你的 /usr/share/fonts/ttf/gb 目錄下是否裝有中文字體 gbsn00lp.ttf 。如果沒有,也可以用 Windows 字體目錄下的 song.ttf 來代替。將字體文件拷貝到 Java 安裝目錄下的 jre/lib/fonts 下。第二,修改 Java 安裝目錄下的 jre/lib/fonts/fonts.dir 文件,將首行的 72 改爲 73,然後添加下列內容:
gbsn00lp.ttf -Arphic Technology Co.-AR PL SungtiL GB-medium-r-normal--0-0-0-0-c-0-gb2312.1980-0
如果你用的是 song.ttf ,則應該將頭部的 gbsn00lp.ttf 改爲 song.ttf
第三,在 Java 安裝目錄下的 jre/lib 目錄中,將字體配置文件 font.properties.zh.Turbo 複製爲 font.properties.zh 。然後修改 font.properties.zh 。將文件中所有的:
-tlc-song-medium-r-normal--*-%d-*-*-c-*-gbk-0
替換爲:
-Arphic Technology Co.-AR PL SungtiL GB-medium-r-normal--0-0-0-0-c-0-gb2312.1980-0
第四,用 JDK 自帶的演示程序 SwingSet2 測試,如果看到中文菜單則說明設置成功。

 

 

六、Java Web配置過濾器進行字符編碼

1.      Web.xml中配置過濾器:

<!-- Example filter to set character encoding on each request -->
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class> com.hthk.iisz.born.filter. SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
        </init-param>
    </filter>
<!-- Example filter mapping to apply the "Set Character Encoding" filter to *all* requests processed by this web application -->
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2.       編寫過濾器

/*

* Licensed to the Apache Software Foundation (ASF) under one or more

* contributor license agreements.  See the NOTICE file distributed with

* this work for additional information regarding copyright ownership.

* The ASF licenses this file to You under the Apache License, Version 2.0

* (the "License"); you may not use this file except in compliance with

* the License.  You may obtain a copy of the License at

*

*     http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

 

package com.hthk.iisz.born.filter;

 

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

/**

 * <p>Example filter that sets the character encoding to be used in parsing the

 * incoming request, either unconditionally or only if the client did not

 * specify a character encoding.  Configuration of this filter is based on

 * the following initialization parameters:</p>

 * <ul>

 * <li><strong>encoding</strong> - The character encoding to be configured

 *     for this request, either conditionally or unconditionally based on

 *     the <code>ignore</code> initialization parameter.  This parameter

 *     is required, so there is no default.</li>

 * <li><strong>ignore</strong> - If set to "true", any character encoding

 *     specified by the client is ignored, and the value returned by the

 *     <code>selectEncoding()</code> method is set.  If set to "false,

 *     <code>selectEncoding()</code> is called <strong>only</strong> if the

 *     client has not already specified an encoding.  By default, this

 *     parameter is set to "true".</li>

 * </ul>

 *

 * <p>Although this filter can be used unchanged, it is also easy to

 * subclass it and make the <code>selectEncoding()</code> method more

 * intelligent about what encoding to choose, based on characteristics of

 * the incoming request (such as the values of the <code>Accept-Language</code>

 * and <code>User-Agent</code> headers, or a value stashed in the current

 * user's session.</p>

 *

 * @author Craig McClanahan

 * @version $Revision: 466607 $ $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct 2006) $

 */

public class SetCharacterEncodingFilter implements Filter {

    // ----------------------------------------------------- Instance Variables

    /**

     * The default character encoding to set for requests that pass through

     * this filter.

     */

    protected String encoding = null;

    /**

     * The filter configuration object we are associated with.  If this value

     * is null, this filter instance is not currently configured.

     */

    protected FilterConfig filterConfig = null;

    /**

     * Should a character encoding specified by the client be ignored?

     */

    protected boolean ignore = true;

    // --------------------------------------------------------- Public Methods

    /**

     * Take this filter out of service.

     */

    public void destroy() {

 

        this.encoding = null;

        this.filterConfig = null;

 

    }

 

 

    /**

     * Select and set (if specified) the character encoding to be used to

     * interpret request parameters for this request.

     *

     * @param request The servlet request we are processing

     * @param result The servlet response we are creating

     * @param chain The filter chain we are processing

     *

     * @exception IOException if an input/output error occurs

     * @exception ServletException if a servlet error occurs

     */

    public void doFilter(ServletRequest request, ServletResponse response,

                         FilterChain chain)

    throws IOException, ServletException {

        // Conditionally select and set the character encoding to be used

        if (ignore || (request.getCharacterEncoding() == null)) {

            String encoding = selectEncoding(request);

            if (encoding != null)

                request.setCharacterEncoding(encoding);

        }

    // Pass control on to the next filter

        chain.doFilter(request, response);

    }

    /**

     * Place this filter into service.

     *

     * @param filterConfig The filter configuration object

     */

    public void init(FilterConfig filterConfig) throws ServletException {

 

    this.filterConfig = filterConfig;

        this.encoding = filterConfig.getInitParameter("encoding");

        String value = filterConfig.getInitParameter("ignore");

        if (value == null)

            this.ignore = true;

        else if (value.equalsIgnoreCase("true"))

            this.ignore = true;

        else if (value.equalsIgnoreCase("yes"))

            this.ignore = true;

        else

            this.ignore = false;

    }

    // ------------------------------------------------------ Protected Methods

    /**

     * Select an appropriate character encoding to be used, based on the

     * characteristics of the current request and/or filter initialization

     * parameters.  If no character encoding should be set, return

     * <code>null</code>.

     * <p>

     * The default implementation unconditionally returns the value configured

     * by the <strong>encoding</strong> initialization parameter for this

     * filter.

     *

     * @param request The servlet request we are processing

     */

    protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }

}

 

七、Tomcat處理中文字符編碼

首先JAVA(包括JSP)源文件中很可能包含有中文,而Java和JSP源文件的保存方式是居於字節流的,如果Java和JSP編譯成class文件過程中,使用的編碼方式與源文件的編碼方式不一致,就會出現亂碼。基於這種亂碼,建議在Java文件中儘量不要寫中文(註釋部分不參與編譯,寫中文沒關係),如果必須寫的話,儘量手動帶參數編譯-ecoding GBK或-ecoding gb2312;對於JSP,在文件頭加上<%@page contentType=”text/html;charset=GBK”%>或<%@page contentType=”text/html;charset=gb2312”%>基本上就能解決這類亂碼問題.

 

Get提交方式的字符編碼修改方法:

更改%TOMCAT_HOME%/conf/server.xml

    <Connector port="8080" maxHttpHeaderSize="8192"

               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

               enableLookups="false" redirectPort="8443" acceptCount="100"

               connectionTimeout="20000" disableUploadTimeout="true"  URIEncoding=’TUF-8’ useBodyEncodingForURI="true"./>

Post提交方式的媳婦編碼修改方法:

   更改%TOMCAT_HOME%/bin/ catalina.bat或catalina.sh

catalina.bat在第51行添加set CATALINA_OPTS=”-Dfile.encoding=UTF-8”

catalina.sh 在第52行添加export CATALINA_OPTS = "-Dfile.encoding=UTF-8"

 

 

八、總結

JAVA Unicode for TOMCAT
1. Edit file <TOMCAT>/conf/server.xml under tag <Connector> to add URIEncoding="UTF-8" useBodyEncodingForURI="true".
2. Edit file <TOMCAT>/bin/catalina.bat for Windows to add set CATALINA_OPTS = "-Dfile.encoding=UTF-8" or <TOMCAT>/bin/catalina.sh for Linux to add export CATALINA_OPTS = "-Dfile.encoding=UTF-8".
3.  Set IDE (for example, NetBean, Eclipse and etc) file setting to UTF-8.
4.  For all the JSPs, add these two lines:
a. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
b.  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5.  Create a filter as below:
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
        private String encoding;
        private FilterConfig filterConfig;
        /**
         * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
         */
        public void init(FilterConfig fc) throws ServletException {
                this.filterConfig = fc;
                this.encoding = filterConfig.getInitParameter("encoding");
        }
        /**
         * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
         *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
         */
        public void doFilter(ServletRequest req, ServletResponse resp,
                        FilterChain chain) throws IOException, ServletException {
                req.setCharacterEncoding(encoding);
                chain.doFilter(req, resp);
        }
        /**
         * @see javax.servlet.Filter#destroy()
         */
        public void destroy() {
        }
}
6.        Edit file ../WEB-INF/web.xml to include filter:
        <filter>
                <filter-name>EncodingFilter</filter-name>
                <filter-class>EncodingFilter</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>


發佈了23 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章