解決JSP中使用request亂碼問(wèn)題_JSP教程
推薦:JSP環(huán)境配置TOMCAT的內(nèi)存和連接數(shù)配置詳解AJAX jsp無(wú)刷新驗(yàn)證碼實(shí)例 如果是使用的catalina.sh(linux)或Catalina.bat(win)啟動(dòng)的: 修改這兩個(gè)文件,加上下面這句: SET CATALINA_OPTS= -Xms64m -Xmx128m 如果使用的wi
JSP顯示中文有亂碼怎么辦,用request得到的用戶輸入的中文怎么是亂碼,把漢字寫(xiě)到數(shù)據(jù)庫(kù)怎么是亂碼,等等一些關(guān)于漢字亂碼的問(wèn)題。其實(shí)這個(gè)問(wèn)題很簡(jiǎn)單,管它漢字不漢字,還是日文,還是其他的什么雙字節(jié)的語(yǔ)言,我們一律把它當(dāng)作UTF-8看待。
(一)request中的雙字節(jié)文字
我們來(lái)實(shí)現(xiàn)在整個(gè)應(yīng)用程序中使用UTF-8編碼工作,之所以選擇UTF-8不僅僅之于上述原因,我們知道java的就是基于在UTF-8之上的,所以我們選擇UTF-8應(yīng)該沒(méi)錯(cuò)
首先把我們的.java, .jsp文件都用UTF-8編碼來(lái)保存,如果以前的沒(méi)有用UTF-8保存也無(wú)所謂,但是建議以后寫(xiě)的都用UTF-8來(lái)保存。
并在.jsp里面寫(xiě):
| 以下是引用片段: <%@page contentType="text/html; charset=UTF-8"%>而不是%@page contentType="text/html; charset=UTF-8"% |
然后在web.xml添加下面一段:
| 以下是引用片段: ... encoding UTF-8 ... |
其中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請(qǐng)求就是以UTT-8編碼的,在JSP程序中就可以使用:request.getParameter("myKey")來(lái)直接得到UTF-8編碼的字符串了,而不需要像這樣:new String(request.getParameter("myKey").getBytes("ISO-8859-1"), "GBK")來(lái)解決那些亂碼了。
分享:高手為你解讀J2EE開(kāi)發(fā)過(guò)程中的異常處理在java里有3種異常類(lèi)型: 1. 檢查型異常,這樣的異常繼承于Excetpion,就是在編譯期間需要檢查,如果該異常被throw,那么在該異常所在的method后必須顯示的throws,調(diào)用該method的地方
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復(fù)習(xí)整理
- JSP腳本元素和注釋復(fù)習(xí)總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實(shí)現(xiàn)
- 網(wǎng)頁(yè)模板:關(guān)于jsp頁(yè)面使用jstl的異常分析
- JSP頁(yè)面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫(kù)的配置方法
- JDBC連接Access數(shù)據(jù)庫(kù)的幾種方式介紹
- 網(wǎng)站圖片路徑的問(wèn)題:絕對(duì)路徑/虛擬路徑
- (jsp/html)網(wǎng)頁(yè)上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對(duì)路徑下的圖片解決方法
JSP教程Rss訂閱編程教程搜索
JSP教程推薦
- 初步接觸學(xué)習(xí)JSP語(yǔ)法基礎(chǔ)
- jsp頁(yè)面中顯示word/excel格式的文檔的方法
- jsp驗(yàn)證碼使用
- 如何實(shí)現(xiàn)javabean的屬性拷貝
- Servlet+Jsp實(shí)現(xiàn)圖片或文件的上傳功能具體思路及代碼
- 解析簡(jiǎn)單計(jì)數(shù)器的并發(fā)問(wèn)題
- 選擇JSF不選Struts的十大理由
- jsp上傳圖片即時(shí)顯示效果代碼
- 對(duì)SUN定義的四種JDBC驅(qū)動(dòng)程序標(biāo)準(zhǔn)簡(jiǎn)介
- 講解Linux系統(tǒng)下JDK、Tomcat的安裝
- 相關(guān)鏈接:
- 教程說(shuō)明:
JSP教程-解決JSP中使用request亂碼問(wèn)題
。