码迷,mamicode.com
首页 > Web开发 > 详细

Web.xml配置----字符过滤器

时间:2016-06-13 15:21:05      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

添加EncodingFilter类实现Filter接口

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class EncodingFilter implements Filter {
    FilterConfig config = null;
    // default to GBK
    private String targetEncoding = "GBK";

    public void init(FilterConfig config) throws ServletException {
        this.config = config;
        this.targetEncoding = config.getInitParameter("encoding");
    }

    public void destroy() {
        config = null;
        targetEncoding = null;
    }

    public void doFilter(ServletRequest srequest, ServletResponse sresponse,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)srequest;
        request.setCharacterEncoding(targetEncoding);
        chain.doFilter(srequest, sresponse);
    }
}

在配置在web.xml中添加
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>xx.xx.xx.EncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>useragents</param-name>
            <param-value>Mac</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Web.xml配置----字符过滤器

标签:

原文地址:http://www.cnblogs.com/lpwbk/p/5580553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!