簡(jiǎn)單介紹JSP中文亂碼處理
1.調(diào)用JSP頁(yè)面顯示亂碼
通過(guò)瀏覽器調(diào)用JSP頁(yè)面,在客戶端瀏覽器中所有的中文內(nèi)容出現(xiàn)亂碼。
解決:
首先確認(rèn)本JSP在編輯器中保存時(shí),使用的是GBK的編碼格式,然后在JSP頁(yè)面的開始部分添加 <%@ pageEncoding="GBK" %>就可以解決中文亂碼問(wèn)題。
2.調(diào)用Servlet頁(yè)面顯示亂碼
通過(guò)瀏覽器調(diào)用Servlet,Servlet在瀏覽器中顯示內(nèi)容出現(xiàn)亂碼
解決:
在Servlet使用response在輸出內(nèi)容之前,先執(zhí)行response.setContentType("text/html;charset=GBK")設(shè)定輸出內(nèi)容的編碼為GBK
3.Post表單傳遞參數(shù)亂碼
通過(guò)JSP頁(yè)面、HTML頁(yè)面或者Servlet中的表單元素提交參數(shù)給對(duì)應(yīng)的JSP頁(yè)面或者Servelt而JSP頁(yè)面或者Servlet接收的中文參數(shù)值亂碼。
解決:
在接收POST提交的參數(shù)之前,使用request.setCharacterEncoding("GBK")設(shè)定接收參數(shù)的內(nèi)容使用GBK編碼
更好的解決方法是使用過(guò)濾器技術(shù)
- package com.htt;
 - 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 Encoding implements Filter {
 - public void destroy() { }
 - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
 - request.setCharacterEncoding("GBK");
 - chain.doFilter(request, response);
 - }
 - public void init(FilterConfig filterConfig) throws ServletException { }
 - }
 - Web.xml文件中的設(shè)置
 - <filter>
 - <filter-name>encoding</filter-name>
 - <filter-class>com.htt.Encoding</filter-class>
 - </filter>
 - <filter-mapping>
 - <filter-name>encoding</filter-name>
 - <url-pattern>/ToCh_zn</url-pattern>
 - </filter-mapping>
 
【編輯推薦】















 
 
 
 
 
 
 