一个简单的JAVA字符集过滤器实现

复制代码 代码如下:
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-name>CharEncodingFilter</filter-name>
<filter-class>dw05prj.util.filter.CharEncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>GBK</param-value>
</init-param>

</filter>
<filter-mapping>
<filter-name>CharEncodingFilter</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);
}
}

jsp技术一个简单的JAVA字符集过滤器实现,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。