javaweb 用拦截器实现编码设置&拦截器执行流程

使用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 中得配置

<!-- 设置全局的编码格式,针对get,post请求,默认编码为UTF-8 -->
  <filter>
  	<filter-name>characterFilter</filter-name>
  	<filter-class>com.mscncn.jsd1404.EncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>characterFilter</filter-name>
  	<!-- 拦截所有的请求,为所有的请求设置编码 -->
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

3. filter 的执行流程

javaweb 用拦截器实现编码设置&拦截器执行流程_第1张图片

测试代码如下:

    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中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>1D</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 设置全局的编码格式,针对get,post请求,默认编码为UTF-8 -->
  <filter>
  	<filter-name>characterFilter</filter-name>
  	<filter-class>com.mscncn.jsd1404.EncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
    <filter>
  	<filter-name>filter1</filter-name>
  	<filter-class>com.mscncn.jsd1404.ServletFilter1</filter-class>
  </filter>
  <filter>
  	<filter-name>filter2</filter-name>
  	<filter-class>com.mscncn.jsd1404.ServletFilter2</filter-class>
  </filter>
    <filter-mapping>
  	<filter-name>characterFilter</filter-name>
  	<!-- 设置编码拦截所有的请求 -->
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>filter1</filter-name>
  	<!-- 只是测试filter的执行流程 -->
  	<url-pattern>/test</url-pattern>
  </filter-mapping>
    <filter-mapping>
  	<filter-name>filter2</filter-name>
  	<!-- 只是测试filter的执行流程 -->
  	<url-pattern>/test</url-pattern>
  </filter-mapping>
  <servlet>
  	<servlet-name>test</servlet-name>
  	<servlet-class>com.mscncn.jsd1404.MyFilterTestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>test</servlet-name>
  	<url-pattern>/test</url-pattern>
  </servlet-mapping>
</web-app>


执行结果:

javaweb 用拦截器实现编码设置&拦截器执行流程_第2张图片


你可能感兴趣的:(javaweb 用拦截器实现编码设置&拦截器执行流程)