jsp代碼:
import javax.servlet.*; import javax.servlet.http.*; public class CharsetFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { HttpServletRequest httpRequest = (HttpServletRequest)request; String method = httpRequest.getMethod().toLowerCase(); if(method.equals("post")) { //如果是post,即表單方法,直接設(shè)置charset即可 request.setCharacterEncoding("UTF-8"); } else if(method.equals("get")) { //如果是get方法 request.setCharacterEncoding("UTF-8"); request = new HttpServletRequestWrapper((HttpServletRequest)request) { public String getParameter(String str) { try { return new String(super.getParameter(str).getBytes("iso-8859-1"),"GBK"); } catch(Exception e) { return null; } } }; } chain.doFilter(request, response); } catch(Exception e){} } public void init(FilterConfig filterConfig) { } }
過(guò)濾器配置:
filter> filter-name>CharFilter/filter-name> filter-class>CharsetFilter/filter-class> /filter> filter-mapping> filter-name>CharFilter/filter-name> url-pattern>/*/url-pattern> dispatcher>REQUEST/dispatcher> /filter-mapping>
標(biāo)簽:紅河 云浮 西藏 江西 青島 貴州 宿州 丹東
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《通過(guò)過(guò)濾器(Filter)解決JSP的Post和Request中文亂碼問(wèn)題》,本文關(guān)鍵詞 通過(guò),過(guò)濾器,Filter,解決,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。