JSP/Servlet頁(yè)面中文顯示為亂碼的解決方式

          經(jīng)過網(wǎng)上搜索和自己的經(jīng)驗(yàn),JSP/Servlet頁(yè)面的中文顯示為亂碼的問題可以細(xì)分為5類:
          1. JSP頁(yè)面顯示亂碼。
          2. Servlet接收Form/Request傳遞的參數(shù)時(shí)顯示為亂碼
          3. JSP接收Form/Request傳遞的參數(shù)時(shí)顯示為亂碼
          4. 用<jsp:forward page="catalog2.html"></jsp:forward>時(shí)頁(yè)面顯示亂碼
          5. 數(shù)據(jù)庫(kù)存取的時(shí)候產(chǎn)生亂碼。
          解決方式:
          1. JSP頁(yè)面顯示亂碼的解決方式:
          第一種方式為在頁(yè)面的開頭加上:
          <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
          <!--這里的 GBK可以由 gb2312代替,此處以GBK為例。下同 -->
          注:有時(shí)候如果不再頁(yè)面開頭加上這句,則頁(yè)面中無法保存中文字符,并提示:中文字符在不能被iso-8859-1字符集mapped,這是由于默認(rèn)情況下,JSP是用iso-8859-1來編碼的,可以在Window->Preferences->General->Content Type選項(xiàng)下,在右邊的窗口選擇Text->Jsp,然后在下面的Default Encoding由默認(rèn)的iso-8859-1改為GBK,然后點(diǎn)擊update即可解決。
          然而這種方式會(huì)帶來一些問題:由于這一句在其他文件include該文件的時(shí)候不能被繼承,所以include它的文件也需要在文件開頭加上這句話,此時(shí)如果用的是pageEncoding="gbk"則會(huì)出現(xiàn)問題。類似于org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html;charset=GBK, new: text/html;charset=gbk).
          類似地,如果兩個(gè)文件一個(gè)用的是gbk,一個(gè)用的是gb2312也會(huì)出現(xiàn)問題。
          另一種更好的解決方式為:
          在項(xiàng)目的web.xml中添加以下片段:
          <!-- 下面的代碼為解決頁(yè)面亂碼問題而加入 -->
          <jsp-config>
                      <jsp-property-group>
                          <description>
                             Special property group for JSP Configuration JSP example.
                          </description>
                          <display-name>JSPConfiguration</display-name>
                          <url-pattern>*.jsp</url-pattern>
                          <el-ignored>true</el-ignored>
                          <page-encoding>GBK</page-encoding>
                          <scripting-invalid>false</scripting-invalid>
                          <include-divlude></include-divlude>
                          <include-coda></include-coda>
                      </jsp-property-group>           
                      <jsp-property-group>
                          <description>
                             Special property group for JSP Configuration JSP example.
                          </description>
                          <display-name>JSPConfiguration</display-name>
                          <url-pattern>*.html</url-pattern>
                          <el-ignored>true</el-ignored>
                          <page-encoding>GBK</page-encoding>
                          <scripting-invalid>false</scripting-invalid>
                          <include-divlude></include-divlude>
                          <include-coda></include-coda>
          </jsp-property-group>
          </jsp-config>
          <!--       添加的代碼結(jié)束 -->
          2. Servlet接收Form/Request傳遞的參數(shù)時(shí)顯示為亂碼的解決方式:
          第一種解決方式為在用到request方法的前面加上這條語句:
          request.setCharacterEncoding("GBK");
          同樣地,這也會(huì)由于頁(yè)面設(shè)置中GbK或gB2312大小寫不同或者采用不同的漢語字符集而發(fā)生錯(cuò)誤。
          另一種更好的解決方式為:添加一個(gè)名為SetCharacterEncodingFilter的filter。
          filter的源文件為(參見apach安裝目錄下\webapps\jsp-examples\WEB-INF\classes\filters中的SetCharacterEncodingFilter.java文件):

          package com.filters;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;
          import javax.servlet.UnavailableException;
          public class SetCharacterEncodingFilter implements Filter {

                   protected String encoding = null;
                   protected FilterConfig filterConfig = null;
                   protected boolean ignore = true;
                   public void destroy() {
                       this.encoding = null;
                       this.filterConfig = null;
                   } 
                   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);
                   }

                  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 String selectEncoding(ServletRequest request) {
                       return (this.encoding);
                   }

          }
          同時(shí)在web.xml中添加一下片段:
          <!-- 為解決亂碼問題而添加 --> 
                    <filter>
                       <filter-name>SetCharacterEncoding</filter-name>
                       <filter-class>com.filters.SetCharacterEncodingFilter</filter-class>
                       <init-param>
                           <param-name>encoding</param-name>
                           <param-value>GBK</param-value>
                       </init-param>
                    </filter>
                   <filter-mapping>
                       <filter-name>SetCharacterEncoding</filter-name>
                       <url-pattern>/*</url-pattern>
                   </filter-mapping>
          <!-- 添加代碼結(jié)束 --> 
          3. JSP接收Form/Request傳遞的參數(shù)時(shí)顯示為亂碼
          當(dāng)我們按照第二種亂碼的解決方式修改了web.xml并添加了filter之后,有時(shí)候并不一定就對(duì)亂碼問題高枕無憂了,有時(shí)候我們會(huì)奇怪的發(fā)現(xiàn)Sevlet接收Form/Request傳遞的參數(shù)可以正常顯示了,但是jsp頁(yè)面接受Form/Request傳遞的參數(shù)卻仍然顯示為亂碼。這是為什么呢?
          對(duì)于我遇到的情況而言,我發(fā)現(xiàn)是由于我在用Form發(fā)送信息的頁(yè)面采用了這樣的html:
          <form action="getParam.jsp" >
          姓名<input type="text" name ="UserName"> <br>
          選出你喜歡吃的水果:
          <input type ="checkbox" name = "checkbox1" value = "蘋果"> 蘋果
          <input type ="checkbox" name = "checkbox1" value = "西瓜"> 西瓜
          <input type ="checkbox" name = "checkbox1" value = "桃子"> 桃子
          <input type ="checkbox" name = "checkbox1" value = "葡萄"> 葡萄
          <input type = "submit" value = "提交">
          </form>
          也就是說沒有指定form的method屬性。而問題就發(fā)生在此,F(xiàn)orm的默認(rèn)mothod屬性為get.
          而get是通過在發(fā)送請(qǐng)求的url后面加?然后加參數(shù)和值來傳遞數(shù)據(jù)的的,編碼格式為ASCII.這就要求我們?cè)趥鬟f的數(shù)據(jù)中有非ASCII字符或是超過了100個(gè)字符,那么你必須使用method="post",否則就會(huì)出現(xiàn)亂碼。
          所以解決方式為:第二種亂碼的解決方式+在發(fā)送頁(yè)面的Form中指定method為post.
          4. 用<jsp:forward page="catalog2.html"></jsp:forward>時(shí)頁(yè)面顯示亂碼的解決方式
          此時(shí)實(shí)際上亂碼的原因和產(chǎn)生其他幾種亂碼的原因不同,它的原因在于我們用eclipse編輯要forward的html或者jsp文件時(shí),采用的編碼是可以產(chǎn)生中文亂碼的編碼而不是GBK或者GB2312.所以解決方式就是把eclipse編輯器的編碼方式改為GBK或者GB2312.
          具體操作方式見:上文紅色字體部分。
          5. 數(shù)據(jù)庫(kù)存取的時(shí)候產(chǎn)生亂碼的解決方式(由于尚未遇到此問題,不知道說的是什么,故此部分完全采用楓之閣的文字。如有侵權(quán),請(qǐng)及時(shí)聯(lián)系我刪除之,謝謝。)
          當(dāng)然,在寫數(shù)據(jù)庫(kù)時(shí),也要保正數(shù)據(jù)庫(kù)的編碼與其它一致:
          我們可以在系統(tǒng)盤windows目錄下的my.ini文件,在文件中插入一行default-character-set=GBK,但上面說了這么多,大家也應(yīng)該明白些了吧,改動(dòng)太多的默認(rèn)設(shè)置不是我的風(fēng)格,因此上,這一行還是不要加的好。
          但不管怎么樣,我們還是要?jiǎng)?chuàng)建一個(gè)基于中文編碼的數(shù)據(jù)庫(kù),當(dāng)然,用客戶端登錄的時(shí)候,某些客戶用自動(dòng)把字體編碼轉(zhuǎn)換成中文編碼。在這里,我想說一下在DOS下創(chuàng)建中文編碼數(shù)據(jù)庫(kù)的方法:
          在進(jìn)入數(shù)據(jù)庫(kù)的時(shí)候,用mysql --default-character-set=gbk -u root -p 這句話進(jìn)入mysql,然后創(chuàng)建數(shù)據(jù)庫(kù),如:create database admin;這樣創(chuàng)建起來的數(shù)據(jù)庫(kù)就是基于中文編碼的了。
          用連接數(shù)據(jù)庫(kù)的時(shí)候,讀出的數(shù)據(jù)也可能是亂碼,解決這個(gè)問題的方法非常簡(jiǎn)單,只要在你建立數(shù)據(jù)庫(kù)連接的時(shí)候把URL設(shè)置成下面這個(gè)樣子就可以了:URL= jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=GBK
          好了,說了這么多,總結(jié)一句話結(jié)束吧,把各種地方的編碼統(tǒng)一起來,那么,所在的亂碼問題就都解決了!

          posted on 2009-04-16 10:22 MichaelLee 閱讀(2379) 評(píng)論(1)  編輯  收藏

          評(píng)論

          # re: JSP/Servlet頁(yè)面中文顯示為亂碼的解決方式[未登錄] 2014-02-24 11:44 longlong

          我按照上面的方法操作,可是還是沒有解決亂碼問題,該怎么辦呢,求教?  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          ====Michael Lee====
          =Java Sofware Engineer=
          =Work @ Beijing=
          ---再煩,也別忘微笑;再急,也要注意語氣;再苦,也別忘堅(jiān)持;再累,也要愛自己!---
          ---低調(diào)做人,你會(huì)一次比一次穩(wěn)??;高調(diào)做事,你會(huì)一次比一次優(yōu)秀---
          ---成功的時(shí)候不要忘記過去;失敗的時(shí)候不要忘記還有未來---

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 扬中市| 体育| 清新县| 蓝田县| 枣阳市| 邯郸县| 唐山市| 正蓝旗| 汉沽区| 江西省| 南皮县| 遵化市| 灵石县| 方正县| 昌邑市| 建阳市| 蓝山县| 周口市| 正镶白旗| 高邮市| 眉山市| 宾阳县| 灌云县| 扎赉特旗| 白朗县| 桂东县| 崇礼县| 佛冈县| 察哈| 阿坝县| 嫩江县| 萨嘎县| 包头市| 县级市| 交城县| 庆元县| 沙坪坝区| 盐池县| 济阳县| 安阳县| 晴隆县|