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

首頁 > 編程 > JSP > 正文

解決JSP中使用request亂碼問題

2024-09-05 00:17:12
字體:
來源:轉載
供稿:網友

JSP顯示中文有亂碼怎么辦,用request得到的用戶輸入的中文怎么是亂碼,把漢字寫到數據庫怎么是亂碼,等等一些關于漢字亂碼的問題。其實這個問題很簡單,管它漢字不漢字,還是日文,還是其他的什么雙字節的語言,我們一律把它當作UTF-8看待。

(一)request中的雙字節文字

我們來實現在整個應用程序中使用UTF-8編碼工作,之所以選擇UTF-8不僅僅之于上述原因,我們知道java的就是基于在UTF-8之上的,所以我們選擇UTF-8應該沒錯
首先把我們的.java, .jsp文件都用UTF-8編碼來保存,如果以前的沒有用UTF-8保存也無所謂,但是建議以后寫的都用UTF-8來保存。

并在.jsp里面寫:

以下是引用片段:
<%@page contentType="text/html; charset=UTF-8"%>而不是%@page contentType="text/html; charset=UTF-8"%

然后在web.xml添加下面一段:

以下是引用片段:

...

Set Character Encoding
com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter

encoding

UTF-8



Set Character Encoding
/*

...


其中com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter的代碼如下:

package com.redv.projects.eduadmin.util.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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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); //
Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
}
}
// 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);
}
}

這樣,我們的request請求就是以UTT-8編碼的,在JSP程序中就可以使用:request.getParameter("myKey")來直接得到UTF-8編碼的字符串了,而不需要像這樣:new String(request.getParameter("myKey").getBytes("ISO-8859-1"), "GBK")來解決那些亂碼了。

共2頁上一頁12下一頁
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚顺县| 甘洛县| 茌平县| 临沭县| 平阴县| 呼图壁县| 苏尼特左旗| 井陉县| 邛崃市| 蓬溪县| 台北县| 义马市| 金乡县| 洛南县| 永川市| 荃湾区| 雅江县| 玉林市| 封丘县| 苍南县| 河东区| 息烽县| 密云县| 普兰店市| 黑河市| 鄂州市| 社旗县| 闽清县| 双桥区| 蒙阴县| 庐江县| 清丰县| 沂南县| 定襄县| 防城港市| 耒阳市| 洛扎县| 和林格尔县| 平罗县| 巴彦县| 桂东县|