javascript和JQuery焦点图和代码特效大全
当前最流行的开源CMS网站系统大全
当前位置:主页 > 编程开发 > JAVA技术 >

一个简单的JAVA字符编码过滤器

来源:IT技术网编辑:一页书发布于:2013-06-24人围观Java编码字符过滤器

一个简单的JAVA字符编码过滤器,并且解决strtus2中文乱码问题:具体实现代码如下:

 

package org.felix.common;
 
import java.io.IOException;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
public class CharacterEncodingFilter implements Filter {
 
protected FilterConfig filterConfig = null;
protected String encoding = "";
public void destroy() {
filterConfig = null;
encoding = null;
}
 
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
if(encoding != null && !"".equals(encoding))
servletRequest.setCharacterEncoding(encoding);
filterChain.doFilter(servletRequest, servletResponse);
}
 
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
 
}
 

 

<filter>
 
        <filter-name>characterEncodingFilter</filter-name>
 
        <filter-class>org.felix.common.CharacterEncodingFilter</filter-class>
 
        <init-param>
 
            <param-name>encoding</param-name>
 
            <param-value>UTF-8</param-value>
 
        </init-param>
 
</filter>
 
<filter-mapping>
 
      <filter-name>characterEncodingFilter</filter-name>
 
      <url-pattern>/*</url-pattern>
 
</filter-mapping>
 

Java,编码,字符,相关的文章
有时间的话来看看IT界的突发事件