/*****************************************java与mysq编码对应****************************************/
java中的常用编码UTF-8;GBK;GB2312;ISO-8859-1;
对应mysql数据库中的编码utf8;gbk;gb2312;latin1
/********************************************过滤器使用*********************************************/
//过滤器设置编码过滤(SetCharacterEncodingFilter.java)
package com.sorc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SetCharacterEncodingFilter extends HttpServlet implements Filter{
private FilterConfig filterConfig;
private String encoding=null;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig){
this.filterConfig=filterConfig;
encoding=filterConfig.getInitParameter("encoding");
}
//Process the request/response pair
public void doFilter(ServletRequest request,ServletResponse response,FilterChain filterChain){
try{
request.setCharacterEncoding(encoding);
filterChain.doFilter(request,response);
} catch(ServletException sx){
filterConfig.getServletContext().log(sx.getMessage());
} catch(IOException iox){
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy(){
}
}
//web.xml配置过滤器方法(web.xmd)
setcharacterencodingfilter
com.sorc.SetCharacterEncodingFilter
encoding
utf8
setcharacterencodingfilter
/*