BaseWeb06-JSP

过滤器

package com.chb.filter;

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 String encoding = null;

    public void destroy() {
        this.encoding = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException
    {
        request.setCharacterEncoding(this.encoding);
        chain.doFilter(request, response);
    }

    /**
     * 初始化参数,从init-param
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding = filterConfig.getInitParameter("encoding");

        if (this.encoding == null || "".equals(encoding.trim())) {
            this.encoding = "UTF-8";
        }
    }

}

在web.xml中设置filter的配置

BaseWeb06-JSP_第1张图片

你可能感兴趣的:(#,jsp)