struts2 编码过滤器代码

java代码

package filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
/**
 *  编码过滤器实体类
 * @author Administrator
 *
 */
public class EncodingFilter implements Filter {
	private  String encoding = "UTF-8";
    
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
	
	@Override
    public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
        HttpServletRequest hreq = (HttpServletRequest)request;
        HttpServletResponse hres = (HttpServletResponse)response;
        hres.setContentType("text/html;charset="+encoding);
        HttpRequestWapper req = new HttpRequestWapper(hreq);
        chain.doFilter(req, response);
    }
     
	@Override
    public void init(FilterConfig filterConfig) throws ServletException {
    	// TODO Auto-generated method stub
        String encodeName = filterConfig.getInitParameter("encoding");
        if(encodeName != null && !"".equals(encodeName.trim())){
            this.encoding = encodeName;
        }
    }
 
    public class HttpRequestWapper extends HttpServletRequestWrapper{
 
        public HttpRequestWapper(HttpServletRequest request) {
            super(request);
             
        }
        //重写方法
        public String getParameter(String name){
            String str = null;
            str = encoding(((HttpServletRequest)this.getRequest()).getParameter(name));
             
            return str;
        }
        public String[] getParameterValues(String name){
            String[] str = null;
            str = ((HttpServletRequest)this.getRequest()).getParameterValues(name);
            if(str != null){
                for(int i = 0; i < str.length; i++){
                    str[i] = encoding(str[i]);
                }
            }
            return str;
        }
         
    }
    //实现转码
    public String encoding(String str){
        if(str != null){
            try {
                str = new String(str.getBytes("iso-8859-1"),this.encoding);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return str;
    }
}

web.xml配置

<!--配置字符编码的过滤器-->
	<filter>
		<filter-name>EncodingFilter</filter-name>
		<filter-class>filter.EncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<!-- 过滤器映射 -->
	<filter-mapping>
		<filter-name>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern><!-- 对所有资源过滤 -->
	</filter-mapping>

注意:当在web.xml配置多个过滤器的时候,一定要把struts的过滤器放到最后,这样可以防止过滤器链被中断,导致你配置的其他过滤器不起作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值