编码过滤器

阅读更多
简单编码过滤器
解决乱码问题,统一用UTF-8编码

一、过滤器EnCodeFilter.java
package com.test.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 EnCodeFilter implements Filter {
private String encode="ISO8895-1";

public void destroy(){

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
System.out.println("编码过滤器:启动UTF-8编码");
}

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

}

二、在web.xml中配置如下:


    EncodeFilter
    com.inpound.filter.EnCodeFilter


    EncodeFilter
    /*

你可能感兴趣的:(filter,servlet,encodefilter,java,web)