注册 登录
编程论坛 J2EE论坛

中问乱码问题(谁能注释一下这段代码)

新手上路了 发布于 2008-06-18 17:35, 1031 次点击
package com.filters;

import
import javax.servlet.*;

//中文过滤器
public class SetCharacterEncodingFilter
    implements Filter
{

    public SetCharacterEncodingFilter()
    {
        encoding = null;
        filterConfig = null;
        ignore = true;
    }

    public void destroy()
    {
        encoding = null;
        filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
    {
        if(ignore || request.getCharacterEncoding() == null)
        {
            String encoding = selectEncoding(request);
            if(encoding != null)
                request.setCharacterEncoding(encoding);
        }
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig)
        throws ServletException
    {
        this.filterConfig = filterConfig;
        encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        if(value == null)
            ignore = true;
        else
        if(value.equalsIgnoreCase("true"))
            ignore = true;
        else
        if(value.equalsIgnoreCase("yes"))
            ignore = true;
        else
            ignore = false;
    }

    protected String selectEncoding(ServletRequest request)
    {
        return encoding;
    }

    protected String encoding;
    protected FilterConfig filterConfig;
    protected boolean ignore;
2 回复
#2
恋轩念伊人2008-06-20 16:04
不懂
#3
高寒2008-06-21 09:13
这是过滤器代码,主要设置编码格式,主要是在doFilter方法中写过滤代码,过滤之后利用chain.doFilter(request, response);沿过滤链继续向下传递
1