CharacterEncodingFilter.java继承了HttpFilter.java用于过滤,是编码格式统一为gbk,但需要在web.xml中配置配置代码为
CharacterEncodingFilter
com.util.CharacterEncodingFilter
CharacterEncodingFilter
/*
REQUEST
FORWARD
INCLUDE
package com.util;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public abstract class HttpFilter implements Filter
{
protected FilterConfig config;
public void init(FilterConfig config) throws ServletException
{
this.config = config;
init();
}
public void init() throws ServletException
{
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
doFilter((HttpServletRequest)request, (HttpServletResponse)response, chain);
}
public abstract void doFilter(HttpServletRequest request,
HttpServletResponse response,
FilterChain chain)
throws IOException, ServletException;
public void destroy()
{
}
public String getInitParameter(String name)
{
return config.getInitParameter(name);
}
public void log(String content)
{
config.getServletContext().log(content);
}
public ServletContext getServletContext()
{
return config.getServletContext();
}
}
package com.util;
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CharacterEncodingFilter extends HttpFilter{
@Override
public void doFilter(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
chain.doFilter(request, response);
}
}