通過過濾器(Filter)解決JSP的Post和Request中文亂碼問題
更新時(shí)間:2014年08月04日 12:50:37 投稿:mdxy-dxy
這篇文章主要介紹了jsp中通過過濾器(Filter)解決JSP的Post和Request中文亂碼問題的方法,需要的朋友可以參考下
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)
{
}
}
過濾器配置:
<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>
相關(guān)文章
jdbc操作mysql數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了jdbc操作mysql數(shù)據(jù)庫的方法,涉及jsp基于jdbc針對(duì)mysql數(shù)據(jù)庫的連接、插入、查詢等簡(jiǎn)單操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
IIS6+TOMCAT整合,實(shí)戰(zhàn)實(shí)例!
IIS6+TOMCAT整合,實(shí)戰(zhàn)實(shí)例!...2007-02-02
實(shí)現(xiàn)jsp驗(yàn)證碼的簡(jiǎn)單小例子
實(shí)現(xiàn)jsp驗(yàn)證碼的簡(jiǎn)單小例子,需要的朋友可以參考一下2013-04-04
實(shí)戰(zhàn) J2EE 開發(fā)購物網(wǎng)站 - 創(chuàng)建數(shù)據(jù)庫
實(shí)戰(zhàn) J2EE 開發(fā)購物網(wǎng)站 - 創(chuàng)建數(shù)據(jù)庫...2006-10-10

