使用Filter 设置编码简化Servlet的开发
1. 核心代码
package com.mscncn.jsd1404;import java.io.IOException;import java.io.UnsupportedEncodingException;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.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;public class EncodingFilter implements Filter { private static String encoding; private static final String DEFAULT_CHARSET="UTF-8"; @Override public void destroy() { System.out.println("--------destroy---------"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("--------doFilter---------"); HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse=(HttpServletResponse)response; if("GET".equals(httpRequest.getMethod())){ EncodingHttpServletRequest wrapper = new EncodingHttpServletRequest(httpRequest, encoding); chain.doFilter(wrapper, response); }else{ httpRequest.setCharacterEncoding(encoding); httpResponse.setContentType("text/html;charset="+encoding); chain.doFilter(request, response); } } private static class EncodingHttpServletRequest extends HttpServletRequestWrapper{ private HttpServletRequest request; public EncodingHttpServletRequest(HttpServletRequest request,String encoding) { super(request); this.request = request; } @Override public String getParameter(String name) { String value = request.getParameter(name); try { value = new String(value.getBytes("iso8859-1"), encoding); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return super.getParameter(name); } } @Override public void init(FilterConfig config) throws ServletException { System.out.println("--------init---------"); encoding = config.getInitParameter("encoding"); if(encoding==null||"".equals(encoding)) encoding=DEFAULT_CHARSET; }}
2. web.xml 中得配置
characterFilter com.mscncn.jsd1404.EncodingFilter encoding UTF-8 characterFilter /*
3. filter 的执行流程
测试代码如下:
servlet
public class MyFilterTestServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(" 执行servlet 中的 service()方法 \t\t\t\t\t"+" 33333333"); }}
第一个拦截器:
package com.mscncn.jsd1404;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 ServletFilter1 implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("第一个拦截器的chain.doFilter()之前 \t\t\t\t\t"+" 111111111"); chain.doFilter(request, response); System.out.println("第一个拦截器的chain.doFilter()之后 \t\t\t\t\t"+" 5555555"); } @Override public void init(FilterConfig arg0) throws ServletException { }}
第二个拦截器:
package com.mscncn.jsd1404;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 ServletFilter2 implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("第二个拦截器的chain.doFilter()之前 \t\t\t\t\t"+" 2222222"); chain.doFilter(request, response); System.out.println("第二个拦截器的chain.doFilter()之后 \t\t\t\t\t"+" 4444444"); } @Override public void init(FilterConfig arg0) throws ServletException { }}
web.xml中的配置:
1D index.html index.htm index.jsp default.html default.htm default.jsp characterFilter com.mscncn.jsd1404.EncodingFilter encoding UTF-8 filter1 com.mscncn.jsd1404.ServletFilter1 filter2 com.mscncn.jsd1404.ServletFilter2 characterFilter /* filter1 /test filter2 /test test com.mscncn.jsd1404.MyFilterTestServlet test /test
执行结果: