java 中文乱码过滤器_JAVA中文乱码过滤器(用java过滤器解决中文乱码)V0422 整理版...
JAVA中文亂碼過(guò)濾器(用java過(guò)濾器解決中文亂碼)V0422 整理版
不用再擔(dān)心java中文亂碼問(wèn)題,一步到位,一次性解決。更方便更快捷。
web-xml配置
EncodingFilter
com.epet.util.EncodingFilter
encoding
GBK
ignore
true
EncodingFilter
/*
用java過(guò)濾器解決中文亂碼
哎,今天終于明白,干什么事都他媽得細(xì)心,學(xué)java更得如此,Java對(duì)大小寫要求,標(biāo)點(diǎn)符號(hào)要求真他媽的嚴(yán)格。廢話不多說(shuō)了,寫點(diǎn)我怎么解決的
1. 首先在你的工程中創(chuàng)建servlet,package為xiaogu ,name為EncodingFilter,然后把下邊代碼粘貼過(guò)去。
package xiaogu;
import java.io.IOException;
import java.io.PrintWriter;
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
public class EncodingFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
private String trargetEncoding=null;
public void init(FilterConfig filterConfig)throws ServletException{
this.filterConfig=filterConfig;
this.trargetEncoding=this.filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
// TODO Auto-generated method stub
try{
//在控制臺(tái)輸出目標(biāo)編碼
System.out.println("trargetEncoding:"+trargetEncoding+"\n");
//執(zhí)行編碼,實(shí)際的處理內(nèi)容
request.setCharacterEncoding(trargetEncoding);
filterChain.doFilter(request,response);
}
catch (ServletException sx)
{
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox){
filterConfig.getServletContext().log(iox.getMessage());
}
}
public void destroy() {
//清空資源
this.filterConfig=null;
this.trargetEncoding=null;
}
}
2.在web.xml中把下面的代碼插到與中間,
encodingfilter
xiaogu.EncodingFilter//xiaogu為你的Java程序的包名,EncodingFilter是你的java的類名。
encode to UTF-8//UTF-8表示中文編碼
encoding
UTF-8
encodingfilter
/*
ok中文亂碼問(wèn)題全部解決。整了一下,分享給大家。
總結(jié)
以上是生活随笔為你收集整理的java 中文乱码过滤器_JAVA中文乱码过滤器(用java过滤器解决中文乱码)V0422 整理版...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 迅雷赚钱宝一代二代(pro)固件大全
- 下一篇: seat TCC 实战(图解_秒懂_史上