本文目錄一覽:
- 1、JSP亂碼一般有什麼解決辦法?
- 2、我的jsp程序出現亂碼了,大家知道為什麼?謝謝了
- 3、關於jsp被tomcat編譯後的網頁出現漢字亂碼。
- 4、jsp頁面中文亂碼,怎麼解決
- 5、jsp中經常出現亂碼,怎麼解決
- 6、怎麼解決jsp中文亂碼問題,我要瘋了
JSP亂碼一般有什麼解決辦法?
URIENcoding 設成GBK或GB2312
2. 表單中或傳遞字元串:本來輸入的漢字是正常的,但是提交後再顯示出來是亂碼,因為提交的一般是 ISO8859編碼,所以顯示的時候要轉成GB2312編碼:
String S=new String(rs.getString(“news”).getBytes(“gb2312″),”ISO8859_1”);
//rs.getString(“news”)為待轉換的字元串
然後使用S字元串的值就可以了
3. 有的伺服器端的語言環境如果設成簡體中文的也可以解決此類問題
4. 插入資料庫中的字元是亂碼
看看資料庫中支持的是何種編碼方式,用類似2中的方式作一下轉換即可。
5. 總之,用jsp開發,碰到亂碼,你得分析是讀的時候發生亂碼,還是寫的時候發生亂碼,用2中的轉換,基本就能解決問題,有些時候寫的時候做一次轉換,例如:
String S=new String(rs.getString(“news”).getBytes(“gb2312″),”ISO8859_1”);
//讀的時候在轉換回來
String S=new String(rs.getString(“news”).getBytes(“ISO8859_1″),”GB2312”);
或者把ISO8859-1和GB2312 的位置換一下,自己多試試,就能找到解決問題的辦法。
將亂碼問題分為三類JSP頁面顯示中文亂碼;表單提交亂碼;資料庫應用亂碼
1) JSP頁面內輸出中文時出現亂碼
解決方案在JSP文件中使用page命令指定響應結果的MIME類型,如%@ page language=”java” contentType=”text/html;charset=gb2312″ %
2)表單提交亂碼
表單提交時(post和Get方法),使用request.getParameter方法得到亂碼,這是因為tomcat處理提交的參數時默認的是iso-8859-1,表單提交get和post處理亂碼問題不同,下面分別說明。
(1)POST處理
對post提交的表單通過編寫一個過濾器的方法來解決,過濾器在用戶提交的數據被處理之前被調用,可以在這裡改變參數的編碼方式,過濾器的代碼如下:
package cn.gov.beijingit.util;
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 SetCharacterEncodingFilter implements Filter {
/**
* 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;
}
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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
web.xml文件加入過濾器
filter
filter-nameEncoding/filter-name
filter-class
cn.gov.beijingit.util.SetCharacterEncodingFilter
/filter-class
init-param
param-nameencoding/param-name
param-valuegbk/param-value
!–gbk或者gb2312或者utf-8–
/init-param
init-param
param-nameignore/param-name
param-valuetrue/param-value
/init-param
/filter
filter-mapping
filter-nameEncoding/filter-name
servlet-name/*/servlet-name
/filter-mapping
* 注意filter元素要放在所有web.xml元素之前。
(2) Get方法的處理
tomcat對post和get的處理方法不一樣,所以過濾器不能解決get的亂碼問題,它需要在其他地方設置。
打開tomcat_home\conf目錄下server.xml文件,找到對8080埠進行服務的Connector組件的設置部分,給這個組件添加一個屬性:URIEncoding=”GBK”。修改後的Connector設置為:
Connector port=”8080″ maxHttpHeaderSize=”8192″
maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
enableLookups=”false” redirectPort=”8443″ acceptCount=”100″
connectionTimeout=”20000″ disableUploadTimeout=”true” URIEncoding=”GBK” /
* 注意修改後重新啟動tomcat才能起作用。
3)資料庫應用的亂碼,本文還是以mysql為例
(1)修改配置文件mysql_home\my.init
將default-character-set=latin1修改為default-character-set=gbk
(2) 對接受的中文字元重新編碼,例
String name=requset.getParameter(“name”);
name = new String(name.getBytes(“gbk”),”iso8859-1″);
4)tomcat5.x的include頁面亂碼
為了能讓tomcat5.x能像tomcat4那樣處理include頁面,需要修改項目web-info/web.xml文件,把默認的字符集設置成gbk/gb2312就可以了,如:
jsp-config
jsp-property-group
descriptionSpecial property group for JSP Configuration JSP example./description
display-nameJSPConfiguration/display-name
nbs
文章出處:DIY部落()
我的jsp程序出現亂碼了,大家知道為什麼?謝謝了
會出現這種情況 一般是你新建了一個JSP頁面沒有先把pageEncoding改為gbk或gb2312
而是在寫了代碼(有中文)後才來改成GBK保存
pageEncoding=”GBK”
如果想不要亂 建完一個頁面的第一件事就是設pageEncoding=”GBK”並保存
再去寫別的代碼
關於jsp被tomcat編譯後的網頁出現漢字亂碼。
%@ page contentType=”text/html; charset=UTF-8″ language=”java” %
然後用記事本打開該jsp文件,另存為,選擇編碼為UTF-8。
jsp頁面中文亂碼,怎麼解決
剛開始學習jsp的程序員都會遇到這樣一個問題,就是網頁上的中文無法顯示.總結了以下幾條方法。
1、在jsp頁中加入一條語句:
%@ page contentType=”text/html;charset=gb2312″ %中文顯示就正常了。
2、對於從網頁中的文本框通過String parameter = request.getParameter(「parameter」);方式獲得的字元串,均是8859_1的編碼,
如果想把它顯示在網頁上就必須得用parameter = new String(parameter.getBytes(「8859_1」),」gb2312」)進行轉換,windows和linux這兩種系統都是一樣的。
有一個簡單方法,就是在 getParameter() 方法獲取參數之前,使用request.setCharacterEncoding(“GB2312”);,將提交的信息轉化為 GB2312 編碼。
3、但是對於將數據寫入資料庫的問題,採取的方式就不同了:
windows下,必須要採用該字元串轉換再插入資料庫,而linux下就不需要,而是直接把8859_1編碼的字元插入。
如果從資料庫中讀出的數據,對於windows因為在插入時已經做了轉換,所以讀出時已經是gb2312的,當把它顯示在網頁上時,不需要做編碼轉換,而 linux上的mysql中的數據是8859_1的所以要做編碼的轉換。
4、 如果你給某個字元串變數賦予一個中文字元串的值,那麼在你取出他時,在網頁上的顯示是不需要做字元轉換的,
但是如果你要在linux下做字元的比較,則還需要把字元做parameter = new String(parameter.getBytes(「gb2312」),」8859_1」)的轉換。
5、長江電力網站解決中文問題的方法是:
1)在catalina.sh文件中的相關位置添加下面一行
-Dfile.encoding=GBK \
2)在每個jsp文件的最前面添加下面兩行
%@ page contentType=”text/html; charset=GBK” %
%request.setCharacterEncoding(“GBK”);%
jsp中經常出現亂碼,怎麼解決
1.最簡單的把頂部改為:%@ page language=”java” import=”java.util.*” pageEncoding=”gbk”%
2.使用Filter:
在工具包中新建一個Filter:EncodingFilter類
代碼如下:
public class EncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding(“gbk”);
resp.setCharacterEncoding(“gbk”);
chain.doFilter(req,resp);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
在web.xml中配置如下:
filter
filter-nameEncoding/filter-name
filter-classcom.ibm.common.EncodingFilter/filter-class
/filter
filter-mapping
filter-nameEncoding/filter-name
url-pattern/*/url-pattern
/filter-mapping
怎麼解決jsp中文亂碼問題,我要瘋了
1、JSP頁面出現的中英文亂碼:
我們的PageCharset.jsp頁面代碼如下所示:
[html] view plain copy
%@ page language=”java” import=”java.util.*”%
html
head
title中文顯示示例/title
/head
body
中文顯示的示例。
%
out.print(“這裡是用jsp輸出的中文”);
%
/body
/html
當我們在保存我們的文件的時候會出現下面的提示:
整因為在我們的MyEclipse中默認的編碼為ISO-8859-1,而ISO-8859-1不支持中文的編碼,所以jsp頁面代碼如果出現中文就不能保存了。對於這樣的錯誤,我們只要在頁面上加上支持中文的編碼格式就可以了,在jsp頁面中加上pageEncoding=「gb2132」 支持中頁面的編碼格式就可以了。這樣我們就能正常保存我們的jsp源文件了。
2、URL傳遞參數中文亂碼
[html] view plain copy
%@ page language=”java” import=”java.util.*” pageEncoding=”gb2312″%
html
head
titleURL傳遞參數中英文處理示例/title
/head
%
String param = request.getParameter(“param”);
%
body
a href=”URLCharset.jsp?param=’中文'”請單擊這個鏈接/a
您提交的這個參數為:%=param %
/body
/html
啟動tomcat運行結果出現url傳遞的中文亂碼:
這裡我們需要配置tomcat伺服器文件,才能解決這個問題。具體方法是,在tomcat的conf目錄下找到server.xml配置文件,找到如下代碼
[html] view plain copy
span style=”font-size:18px” Connector port=”8080″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ //span
在後面添加上編碼方式,URIEncoding=”gb2312″ 重新啟動tomcat問題就解決了。
3、表單提交中問亂碼
對於表單中提交的數據,可以用request.getPraramter(“”);方法來獲取,但是當表單中出現中文數據的時候就會出現亂碼。
我們的提交表單的頁面,FormCharset.jsp頁面如下:
[html] view plain copy
%@ page language=”java” contentType=”text/html; charset=GB18030″
pageEncoding=”GB18030″%
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=GB18030″
titleForm中文處理示例/title
/head
body
下面是表單內容:
form action=”AcceptFormCharset.jsp” method=”post”
用戶名:input type=”text” name=”userName” size=”10″ /
密 碼:input type=”password” name=”password” size=”10″/
input type=”submit” value=”提交”/
/form
/body
/html
我們的AcceptFormCharset.jsp頁面:
[html] view plain copy
%@ page language=”java” contentType=”text/html; charset=GB18030″
pageEncoding=”GB18030″%
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=GB18030″
titleForm中文處理示例/title
/head
body
下面是表單提交以後request取到的表單的數據:br
%
out.println(“表單輸入的userName值為:” + request.getParameter(“userName”) +”br”);
out.println(“表單輸入的pasword值為:” +request.getParameter(“password”) + “br”);
%
/body
/html
提交表單:
結果如下:
我們看到我們在表單中的中文數據出現了亂碼,為什麼楚翔這種情況呢?是因為我們的tomcat中,對於以post方式提交的表單編碼格式默認為ISO-8859-1的編碼格式,而這種編碼格式是不能編碼中文的,所以就會出現亂碼的現象了。對於這種情況,我們可以對錶單中的數據進行處理,在取得表單參數的時候設置編碼方式,我們更改了我們的接受表單數據的頁面如下所示:
[html] view plain copy
%
String userName = request.getParameter(“userName”);
String passWord = request.getParameter(“password”);
out.println(“表單輸入的userName值為:” + new String(userName.getBytes(“ISO-8859-1″),”gb2312″)+”br”);
out.println(“表單輸入的pasword值為:” + new String(passWord.getBytes(“ISO-8859-1″),”gb2312″)+”br”);
%
這樣就得到我們想要的效果啦:
4、資料庫操作中文亂碼
我們在建立資料庫的時候,最好是能選擇支持中文編碼格式,最好是能和jsp頁面編碼格式保持一致,這樣就儘可能的減少資料庫操作中文亂碼的問題,最開始的連接資料庫的時候,編寫好資料庫的編碼策略,也就是使用這種形式的URL:jdbc:Oracle:thin:@localhost:1521:TEST;userEnicode=true;characterEncoding=gb2312; 這樣我們選擇的資料庫的編碼和我們的jsp編碼就一致了。
寫入到資料庫的時候,資料庫中中文亂碼:
但是如果我們在最開始的時候沒有對資料庫的編碼進行設置,並且我們的資料庫中已經有大量的數據的話,我們再向資料庫中寫入中文數據,資料庫中的中文顯示為亂碼。在寫入數據的時候出現亂碼,是因為我們在處理表單的時候沒有對字元的編碼設置,對於這種情況,我們在jsp中或servlet中加入:
rquest.setCharacterEncoding(“gb2312”);//處理表單請求的時候設置編碼。
這樣再看我們的資料庫,插入的中文欄位就不會亂碼了。
從資料庫中讀出中文亂碼:
資料庫中的中文出現亂碼,就是在讀取資料庫的時候進行轉碼,這樣顯示就不會亂碼了。我們整理這樣的一個轉碼函數:
public String encoder(String str) throws UnsupportedEncodingException
{
String result = new String(str.getBytes(“ISO-ISO-8859-1)”),”gb2312″);
}
5 在myeclipse開發工具中打開中文顯示亂碼
在myeclipse中默認的編碼方式為ISO-8859-1,所以打開有其他編譯器編碼的jsp頁面就會出現亂碼,是因為兩個編譯器保存源文件編碼格式不同,在UltralEdit可以支持中文,但是在Eclipse對jsp文件的保存方式為ISO-8895-1,這種編碼不支持中文,所以就會出現亂碼。
對於這種情況,我們可以更改myeclipse默認的編碼方案,myeclipse-Window-Preferences-General-Content types-Test-JSP
這樣問題就解決啦!
6 Filter批量設置編碼格式
我們對於每一個jsp或servlet我們都要設置編碼格式,效率有些低,我們的servlet的Filter解決了我們的問題。在前篇寫過Filter的解決中文亂碼問題的文章,在這裡就不贅述了。
需要強調的一點,開始使用Java model1模型的時候,我們在web.xml中配置只需要配置好jsp頁面就可以了,在model2模型中我們使用servlet作為控制器,我們就需要在Filter的配置文件web.xml中配置好servlet的設置,對所有的servlet處理的表單編碼進行設置。
[html] view plain copy
filter
filter-nameCharsetEncodingFilter/filter-name
filter-classcom.bjpowernode.drp.util.filter.CharsetEncodingFilter/filter-class
init-param
param-nameencoding/param-name
param-valueGBK/param-value
/init-param
/filter
filter-mapping
filter-nameCharsetEncodingFilter/filter-name
url-pattern*.jsp/url-pattern
/filter-mapping
filter-mapping
filter-nameCharsetEncodingFilter/filter-name
url-pattern/servlet/*/url-pattern
/filter-mapping
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/289235.html