欢迎来到Java学习网!
设为首页 | 加入收藏
当前位置: 首页 > Java语言 > JSP教程 >

一、字符编码过滤器类 EncodingFilter 如下:

package cc.javaweb.api.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 EncodingFilter implements Filter {
private String charset = "UTF-8";

public void destroy() {
}

public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding(charset);
arg1.setCharacterEncoding(charset);
arg2.doFilter(arg0, arg1);
}

public void init(FilterConfig arg0) throws ServletException {
}
}

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

<filter>
<filter-name>encoding</filter-name>
<filter-class>cc.javaweb.api.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/manage/*</url-pattern>
</filter-mapping>

    相关教程
    推荐内容