国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

揭秘5種JSP頁面顯示為亂碼的解決方法

2024-09-05 00:17:14
字體:
供稿:網(wǎng)友
JSP編程中網(wǎng)頁顯示出現(xiàn)亂碼的情況,基本可以歸為5類:
1. JSP頁面顯示亂碼。
2. Servlet接收Form/Request傳遞的參數(shù)時顯示為亂碼
3. JSP接收Form/Request傳遞的參數(shù)時顯示為亂碼
4. 用<jsp:forward page="catalog2.html"></jsp:forward>時頁面顯示亂碼
5. 數(shù)據(jù)庫存取的時候產(chǎn)生亂碼。

下面給出全部解決方法: 
1. JSP頁面顯示亂碼。
第一種為在頁面的開頭加上:
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<!--這里的 GBK可以由 gb2312代替,此處以GBK為例。下同 -->
注:有時候如果不再頁面開頭加上這句,則頁面中無法保存中文字符,并提示:中文字符在不能被iso-8859-1字符集mapped,這是由于默認情況下,JSP是用iso-8859-1來編碼的,可以在Window->Preferences->General->Content Type選項下,在右邊的窗口選擇Text->Jsp,然后在下面的Default Encoding由默認的iso-8859-1改為GBK,然后點擊update即可解決。
然而這種方式會帶來一些問題:由于這一句在其他文件include該文件的時候不能被繼承,所以include它的文件也需要在文件開頭加上這句話,此時如果用的是pageEncoding="gbk"則會出現(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).
類似地,如果兩個文件一個用的是gbk,一個用的是gb2312也會出現(xiàn)問題。
另一種更好的解決方式為:
在項目的web.xml中添加以下片段:
<!-- 下面的代碼為解決頁面亂碼問題而加入 --> 
<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-prelude></include-prelude>  
                <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-prelude></include-prelude>  
                <include-coda></include-coda>  
</jsp-property-group>  
</jsp-config>  
<!--       添加的代碼結(jié)束 --> 

2. Servlet接收Form/Request傳遞的參數(shù)時顯示為亂碼的解決方式:
第一種解決方式為在用到request方法的前面加上這條語句:
request.setCharacterEncoding("GBK");
同樣地,這也會由于頁面設(shè)置中GbK或gB2312大小寫不同或者采用不同的漢語字符集而發(fā)生錯誤。
另一種更好的解決方式為:添加一個名為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); 
         } 


同時在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ù)時顯示為亂碼

當(dāng)我們按照第二種亂碼的解決方式修改了web.xml并添加了filter之后,有時候并不一定就對亂碼問題高枕無憂了,有時候我們會奇怪的發(fā)現(xiàn)Sevlet接收Form/Request傳遞的參數(shù)可以正常顯示了,但是jsp頁面接受Form/Request傳遞的參數(shù)卻仍然顯示為亂碼。這是為什么呢?
對于我遇到的情況而言,我發(fā)現(xiàn)是由于我在用Form發(fā)送信息的頁面采用了這樣的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的默認mothod屬性為get.
而get是通過在發(fā)送請求的url后面加?然后加參數(shù)和值來傳遞數(shù)據(jù)的的,編碼格式為ASCII.這就要求我們在傳遞的數(shù)據(jù)中有非ASCII字符或是超過了100個字符,那么你必須使用method="post",否則就會出現(xiàn)亂碼。
所以解決方式為:第二種亂碼的解決方式+在發(fā)送頁面的Form中指定method為post.
4. 用<jsp:forward page="catalog2.html"></jsp:forward>時頁面顯示亂碼的解決方式
此時實際上亂碼的原因和產(chǎn)生其他幾種亂碼的原因不同,它的原因在于我們用eclipse編輯要forward的html或者jsp文件時,采用的編碼是可以產(chǎn)生中文亂碼的編碼而不是GBK或者GB2312.所以解決方式就是把eclipse編輯器的編碼方式改為GBK或者GB2312.
具體操作方式見:上文紅色字體部分。
5. 數(shù)據(jù)庫存取的時候產(chǎn)生亂碼的解決方式
當(dāng)然,在寫數(shù)據(jù)庫時,也要保正數(shù)據(jù)庫的編碼與其它一致:
我們可以在系統(tǒng)盤windows目錄下的my.ini文件,在文件中插入一行default-character-set=GBK,但上面說了這么多,大家也應(yīng)該明白些了吧,改動太多的默認設(shè)置不是我的風(fēng)格,因此上,這一行還是不要加的好。 
但不管怎么樣,我們還是要創(chuàng)建一個基于中文編碼的數(shù)據(jù)庫,當(dāng)然,用客戶端登錄的時候,某些客戶用自動把字體編碼轉(zhuǎn)換成中文編碼。在這里,我想說一下在DOS下創(chuàng)建中文編碼數(shù)據(jù)庫的方法: 
在進入數(shù)據(jù)庫的時候,用mysql --default-character-set=gbk -u root -p 這句話進入mysql,然后創(chuàng)建數(shù)據(jù)庫,如:create database admin;這樣創(chuàng)建起來的數(shù)據(jù)庫就是基于中文編碼的了。
用連接數(shù)據(jù)庫的時候,讀出的數(shù)據(jù)也可能是亂碼,解決這個問題的方法非常簡單,只要在你建立數(shù)據(jù)庫連接的時候把URL設(shè)置成下面這個樣子就可以了:URL= jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=GBK 
最后總結(jié),把各種地方的編碼統(tǒng)一起來,所在的亂碼問題就都解決了。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 土默特右旗| 西安市| 西华县| 那曲县| 海兴县| 灵山县| 刚察县| 牡丹江市| 峨眉山市| 文安县| 禹州市| 阜新市| 长岛县| 井研县| 佛山市| 襄城县| 秭归县| 汶川县| 天祝| 兰坪| 雷山县| 柏乡县| 威信县| 沅江市| 图片| 金门县| 景洪市| 建阳市| 台山市| 濮阳县| 海伦市| 丰宁| 保康县| 淮阳县| 秦皇岛市| 临高县| 灵璧县| 疏附县| 曲沃县| 古浪县| 肥乡县|