下面图老师小编跟大家分享一个简单的JAVA字符集过滤器实现,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~
【 tulaoshi.com - Web开发 】
代码如下:
package dw05prj.util.filter;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
/*页面字符集过滤流*/
/*
在web.xml中配制的例子
filter
filter-nameCharEncodingFilter/filter-name
filter-classdw05prj.util.filter.CharEncodingFilter/filter-class
init-param
param-namecharset/param-name
param-valueGBK/param-value
/init-param
/filter
filter-mapping
filter-nameCharEncodingFilter/filter-name
url-pattern/*/url-pattern
/filter-mapping
*/
public class CharEncodingFilter implements Filter {
private String charSet ;
public void init(FilterConfig parm1) throws ServletException {
charSet = parm1.getInitParameter("charset") ;
if (charSet == null && charSet.length() 1)
{
charSet = "UTF-8" ;
}
System.out.println ("CharEncodingFilter--字符集: " + this.getCharSet()) ;
}
public void doFilter(ServletRequest parm1, ServletResponse parm2,
FilterChain parm3) throws IOException, ServletException {
// 设定字符集
parm1.setCharacterEncoding(this.getCharSet());
parm3.doFilter(parm1, parm2);
//页面中每个向服务器的请求都被过滤 -- 如 图片,js,HTML/JSP等等
//System.out.println ("---CharEncodingFilter: " + this.getCharSet()) ;
}
public void destroy() {
// TODO: Add your code here
this.setCharSet(null) ;
}
public void setCharSet(String charSet) {
this.charSet = charSet;
}
public String getCharSet() {
return (this.charSet);
}
}
来源:http://www.tulaoshi.com/n/20160219/1596773.html
看过《一个简单的JAVA字符集过滤器实现》的人还看了以下文章 更多>>