JAVA中文乱码过滤器(用java过滤器解决中文乱码)V0422 整理版
不用再担心java中文乱码问题,一步到位,一次性解决。更方便更快捷。
web-xml配置
EncodingFilter
com.epet.util.EncodingFilter
encoding
GBK
ignore
true
EncodingFilter
/*
用java过滤器解决中文乱码
哎,今天终于明白,干什么事都他妈得细心,学java更得如此,Java对大小写要求,标点符号要求真他妈的严格。废话不多说了,写点我怎么解决的
1. 首先在你的工程中创建servlet,package为xiaogu ,name为EncodingFilter,然后把下边代码粘贴过去。
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{
//在控制台输出目标编码
System.out.println("trargetEncoding:"+trargetEncoding+"\n");
//执行编码,实际的处理内容
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中文乱码问题全部解决。整了一下,分享给大家。