Black Horse DAY14 Filter Overview & Life cycle & execution Process

Source: Internet
Author: User

Filters: When accessing a Web resource, the filter is intercepted before you access the Web resource ... After the release ... filter filter can also do some other things.

Steps to write a filter:

1. Write a filter class to implement the filter interface

2. Register in the Web. xml file.

Case 1:

(1). Filter class

Package Cn.itheima.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 FilterDemo1 implements filter {///filter Initialize when calling this method public void init ( Filterconfig filterconfig) throws Servletexception {System.out.println ("init ....");} public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {System.out.println ("Filter ....");//release Chain.dofilter (request, response);} Filter object to do after the death of the work of public void Destroy () {System.out.println ("destory ...");}
(2) configuration in the. web. xml file:

<filter>  <filter-name>FilterDemo1</filter-name>  <filter-class> cn.itheima.filter.filterdemo1</filter-class>  </filter>  <filter-mapping>  < filter-name>filterdemo1</filter-name>  <url-pattern>/*</url-pattern>  </ Filter-mapping>
(3). Write a servlet to test:

Package Cn.itheima.servlet;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 FilterDemo1 extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {response.getwriter (). Write ("Hello world!");} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
Operation Result:

When we start Tomcat, the Init method executes when the load DAY14 application is complete ....

When we visit the FilterDemo1 .... If the release is Dofilter () ... You will see the word Hello World on the page. If there is no release, you will not see ...

When we close Tomcat, we find that the Destory method executes ....

So we can summarize the life cycle of the filter:

Life cycle:

When the server starts, the Web app loads, immediately creates all the filters in the Web app, and immediately calls the Init method to perform the initialization operation when the filter is created.
After it has been created, it resides in memory for subsequent interception services. Each interception to a request causes the Dofilter method to execute.
When the server shuts down or the Web app is removed from the container, the destruction filter object is destroyed as the Web app is destroyed. Before destroying, call the Destory method to perform the aftercare work.
Case 2:

(1). We write two filters FilterDemo1 and FilterDemo2

Package Cn.itheima.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 FilterDemo1 implements filter {///filter Initialize when calling this method public void init ( Filterconfig filterconfig) throws servletexception {}public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {System.out.println ("Filterdemo1....before"); Chain.dofilter (request, response); System.out.println ("Filterdemo1....after");} Filter objects when they die, the aftermath works. public void Destroy () {}}


Package Cn.itheima.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 FilterDemo2 implements Filter {public void init (filterconfig Filterconfig) throws servletexception {}public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {System.out.println ("Filterdemo2....before"); chain.dofilter (Request, response); System.out.println ("Filterdemo2....after");} public void Destroy () {}}

The results of the console printing are:

Filterdemo1....before
Filterdemo2....before
Filterdemo2....after
Filterdemo1....after

The order in which the filters are executed is performed according to the configuration order of the mapping in the Web. xml file.

The Init method can also use Filterconfig to obtain information from the configuration file. I'm not going to show you here.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Black Horse DAY14 Filter Overview & Life cycle & execution Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.