关闭

J2EE 过滤器Filter

310人阅读 评论(0) 收藏 举报
分类:

 

 

如图所示,对于一个指定了过滤器的Web资源(Servlet、JSP、HTML)来说,过滤器拦在客户端与Web资源之间,进出都要经过过滤器。

当请求到来时,先经过过滤器,过滤器可以检查、修改请求的内容再发给对应的Web资源,甚至可以根据请求的内容禁止客户端访问对应的Web资源。

Web资源产生的应答也要先经过过滤器,过滤器可以检查、修改应答的内容再发给客户端。

 

Filter接口

过滤器必须实现javax.servlet.Filter接口,这个接口包含3个方法:init、doFilter、destroy。

init(FilterConfig filterConfig):Java EE容器创建过滤器实例后调用这个方法,用于为过滤器做准备工作,可以从filterConfig对象读取配置文件web.xml中为该过滤器设置的初始化参数。

doFilter(ServletRequest request, ServletResponse response, FilterChain chain):过滤操作在这个方法里实现,包括检查、修改请求对象,检查修改应答对象。参数request是请求对象,response是应答对象,chain用于访问后续过滤器。

destroy():Java EE容器在销毁过滤器实例前调用这个方法,用于释放资源。

 
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>myweb</display-name>
	<filter>
		<filter-name>filter1</filter-name>
		<filter-class>hb.filter.Filter1</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>filter1</filter-name>
		<url-pattern>/filter/*</url-pattern>
	</filter-mapping>
	
	<filter>
		<filter-name>filter2</filter-name>
		<filter-class>hb.filter.Filter2</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>filter2</filter-name>
		<url-pattern>/filter/*</url-pattern>
	</filter-mapping>
</web-app>
 
package hb.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 Filter1 implements Filter {


	public void destroy() {
		System.out.println("Filter1 destory");
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException {
		System.out.println("Filter1 doFilter");
		chain.doFilter(arg0, arg1);
	}

	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("Filter1 init");
	}

}
 
package hb.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 Filter2 implements Filter{
	
	public void destroy() {
		System.out.println("Filter2 destory");
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
		System.out.println("Filter2 doFilter");
	}

	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("Filter2 init");
	}
}
 
启动服务器,运行结果如下:
1、启动服务器,打印
Filter1 init
Filter2 init
2、在浏览器地址栏中输入 http://localhost:8080/myweb/filter/  打印
Filter1 doFilter
Filter2 doFilter
 
1、以url-pattern方式配置的filter运行时肯定是先于servlet-name方式配置的filter
2、以url-pattern方式配置的filter中,如果有多个与当前请求匹配,则按web.xml中filter-mapping出现的顺序来运行
3、对于以servlet-name方式配置的filter,如果有多个与当前请求匹配,也是按web.xml中filter-mapping出现的顺序来运行
 
<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>xx.MyFilter</filte-class>
</filter>
<filter-mapping>
	<filter-name>myFilter</filter-name>
	<servlet-name>目标资源一</servlet-name>
	<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
	<filter-name>myFilter</filter-name>
	<servlet-name>目标资源二</servlet-name>
	<dispatcher>REQUEST</dispatcher>
</filter-mapping>
 
这种方式的配置,说明只有直接访问该目标资源时该过滤器才会起作用,对转发到该目标资源的请求将忽略不处理。
那如果我想对转发到目标资源二的请求进行过滤,那怎么办呢?答案见,下一种过滤器,forward过滤器。
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>myweb</display-name>
	<servlet>
		<display-name>MyServlet</display-name>
		<servlet-name>MyServlet</servlet-name>
		<servlet-class>hb.filter.MyServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>MyServlet</servlet-name>
		<url-pattern>/MyServlet.do</url-pattern>
	</servlet-mapping>
	
	<filter>
		<filter-name>filter3</filter-name>
		<filter-class>hb.filter.Filter3</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>filter3</filter-name>
		<servlet-name>MyServlet</servlet-name>
	</filter-mapping>
</web-app>
 
package hb.filter;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MyServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("servlet");
		response.sendRedirect("index.jsp");
	}

}
 
package hb.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 Filter3 implements Filter {


	public void destroy() {
		System.out.println("Filter3 destory");
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException {
		System.out.println("Filter3 doFilter");
		chain.doFilter(arg0, arg1);//不能少,否则无法到servlet处理
	}

	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("Filter3 init");
	}

}
 
浏览器地址栏中输入:http://localhost:8080/myweb/MyServlet.do
打印结果:
Filter3 doFilter
servlet
__________________-------------------
 
  • 大小: 4.9 KB
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:280367次
    • 积分:8435
    • 等级:
    • 排名:第2299名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:4条
    最新评论