在一个Web应用程序中可以注册多个Filter程序,每个Filter 程序都可以针对某-一个 URL进行拦截, 但是多个Filter 映射配置影响到了请求处理的不同。

在一个Web应用程序中可以注册多个Filter程序,每个Filter 程序都可以针对某-一个 URL进行拦截, 但是多个Filter 映射配置影响到了请求处理的不同。_第1张图片
MyFilter3类

package com.jinglan.cn;
import java.io.IOException;
import java.io.PrintWriter;
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 MyFilter3 implements Filter {
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		PrintWriter out = response.getWriter();
		out.println("My Filter3 before");
		chain.doFilter(request, response);
		out.println("My Filter3 after");	
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
	}
}

MyFilter4类

package com.jinglan.cn;
import java.io.IOException;
import java.io.PrintWriter;
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 MyFilter4 implements Filter {
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		PrintWriter out = response.getWriter();
		out.println("My Filter4 before");
		chain.doFilter(request, response);
		out.println("My Filter4 after");
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}

FilterChain类

package com.jinglan.cn;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FilterChain extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("FilterChain");
	}
}

xml文件配置


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>FilterTestdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
   <servlet>
  	<servlet-name>FilterChainservlet-name>
  	<servlet-class>com.jinglan.cn.FilterChainservlet-class>
  servlet>
  <servlet-mapping>
  	<servlet-name>FilterChainservlet-name>
  	<url-pattern>/FilterChainurl-pattern>
  servlet-mapping>
    <filter>
  	<filter-name>MyFilter3filter-name>
  	<filter-class>com.jinglan.cn.MyFilter3filter-class>
  filter>
  <filter-mapping>
  	<filter-name>MyFilter3filter-name>
  	<url-pattern>/FilterChainurl-pattern>
  filter-mapping>
  <filter>
  	<filter-name>MyFilter4filter-name>
  	<filter-class>com.jinglan.cn.MyFilter4filter-class>
  filter>
  <filter-mapping>
  	<filter-name>MyFilter4filter-name>
  	<url-pattern>/FilterChainurl-pattern>
  filter-mapping>
 
web-app>

【运行结果】
在一个Web应用程序中可以注册多个Filter程序,每个Filter 程序都可以针对某-一个 URL进行拦截, 但是多个Filter 映射配置影响到了请求处理的不同。_第2张图片
【源代码】
https://download.csdn.net/download/muyuxifeng/87766364

你可能感兴趣的:(前端,servlet,java)