Filter 1: Introduction to the filter; create the first Filter;

This blog mainly introduces the basic content of filters, and writes a small example of filters at the same time.

table of Contents

One: Introduction to filters

1. What is a filter?

2. The filter execution process:

3. Three elements of filter development:​

Two: filter example

1. Create a filter

2. Test whether the filter is successfully written


One: Introduction to filters

1. What is a filter?

Air purifier, water purifier, station security system: filter certain things;

Moreover, there are often many levels in these filtering processes, which are "filtered" many times; for example, there are three "filters" in the aircraft security check, which will first check the baggage, then check the carry-on package, and then check the body. If you fail to pass any check, you will not be able to board the plane .

If in JavaWeb: Send a lot of requests to a web application, some requests meet the requirements, some requests do not meet the requirements, Java provides filters to process them.

For example, some requests are imperfect and need to be processed after the filter is intercepted and then passed back; ;; while some requests cannot be released, chain.doFilter() is not applicable to such requests to achieve the effect of interrupting the interception;

Any Servlet has a built-in filter: It can be understood that the prepared filter can be directly applied to the Servlet, and the bottom layer has been processed. . . .

2. The filter execution process:

3. Three elements of filter development:


Two: filter example

1. Create a filter

The first step: create a web project, and write a class to implement the Filter interface: that is, write a filter class

 

MyFirstFilter: To be a filter class, a class needs to implement the javafx.servlet.Filter interface ; the class that implements the Filter interface must implement the init(), doFilter(), destroy() methods. Focus on the doFilter() method, this method needs us to write, here for simple demonstration purposes, we only let the doFilter() method print "filter has been executed" on the console.

package com.imooc.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 MyFristFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * doFilter方法三个参数:ServletRequest:请求对象;ServletResponse:响应对象;FilterChain:过滤链对象
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("过滤器已执行");
		chain.doFilter(request, response);  // 将请求与响应对象随着过滤链依次向后传递,如果不写这句话,请求是无法向后被正常地传递的;
		
	}

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

}

Step 2: After the filter is written, it needs to be configured in web.xml: here for simple demonstration purposes, url-pattern is set to /*, that is, all urls are blocked;

<?xml version="1.0" encoding="UTF-8"?>
<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>first-filter</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>
  
  <!-- filtler标签用于说明哪个类是过滤器,并在应用启动时自动加载,即自动实例化这个类的对象,并对其进行加载 -->
  <filter>
  	<filter-name>MyFrist</filter-name>
  	<filter-class>com.imooc.filter.MyFristFilter</filter-class>
  </filter>
  <!-- 
  	filter-mapping标签用于说明过滤器对URL应用的范围:
  		1.filter-name 过滤器别名,需要与上面<filter>标签中定义的地方保持一致;
  		2.url-pattern 说明过滤器作用范围,/*代表对所有URL进行过滤;(/*也听常用的)
   -->
  <filter-mapping>
  	<filter-name>MyFrist</filter-name>
  	<url-pattern>/*</url-pattern>  <!-- /*代表对所有的url请求都进行拦截 -->
  </filter-mapping>
</web-app>

At this point, a filter has been written!

……………………………………………………

2. Test whether the filter is successfully written

To test the filter, add two resources: index.html and HelloServlet: (Naturally, the URLs for accessing these two resources are also different, just to correspond to the /* above)

Run the program:

Visit index.html:

Discovery console

…………………………

Visit HelloServlet:


Note: (1) It can be seen that after the filter class is written and configured in web.xml, the filter is not set in Servlet or html. It is estimated that this work is completed by the Tomcat server ... This should also be confirmed The beginning of the blog said, "Any Servlet has a built-in filter: It can be understood that the prepared filter can be directly applied to the Servlet, and the bottom layer has been processed..." This sentence.

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/114240538