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) 編輯 收藏