servlet项目 设置编码格式

在web.xml 里面设置


Set Character Encoding
filter.SetEncodingFilter

encoding
UTF-8



Set Character Encoding
/*


然后创建一个设置编码格式的类叫做:SetEncodingFilter

package filter;

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

/**
 * Created by  on 2017/3/23.
 */
public class SetEncodingFilter implements Filter {
    protected String encoding = null;//定义缺省字符编码方式
    protected boolean ignore = true;//定义客户端指定的编码方式是否应被忽略
    protected FilterConfig filterConfig = null;//定义过滤器配置对象,若为null,则说明过滤器未配置

    public void destroy()//停止过滤器的工作
    {
        this.encoding = null;
        this.filterConfig = null;
    }

    //设置字符编码
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        if (ignore || (req.getCharacterEncoding() == null)) {
            req.setCharacterEncoding(encoding);
        }
        chain.doFilter(req, res);
    }

    //启动过滤器
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        if (value == null) this.ignore = true;
        else this.ignore = value.equalsIgnoreCase("true")
                || value.equalsIgnoreCase("yes");
    }

}

编码就在 里面设置,一般为UTF-8与前端的编码对应就好。





 
  
 
 

你可能感兴趣的:(Java)