Java Web Learning Summary (a) filter filter

Source: Internet
Author: User
Tags java web

One, Filter introduction

Filter is also known as filters, filter is the client access to the resource filtering, in line with the conditional release, not meet the conditions of release, and the target resources can be accessed before and after the logical processing.

Two, filter development steps

1) Write a filter class to implement the filter interface

2) Implement methods that are not yet implemented in the interface (focus on implementing the Dofilter method)

3) configuring in Web. XML (primarily configuring which resources to filter)

For example:

Filterdemo.java

Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classFilterdemoImplementsfilter{@Override Public voiddestroy () {System.out.println ("----Filter Destruction----"); } @Override Public voidDoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2)throwsIOException, servletexception {System.out.println ("----Filter Processing----"); //ReleaseArg2.dofilter (arg0, arg1); } @Override Public voidInit (Filterconfig arg0)throwsservletexception {System.out.println ("----Filter Initialization----"); }}

Xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns: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>Filter</Display-name>    <welcome-file-list>        <Welcome-file>index.jsp</Welcome-file>    </welcome-file-list>        <!--Configure Filters -    <Filter>        <Filter-name>Filterdemo</Filter-name>        <Filter-class>Com.zender.FilterDemo</Filter-class>    </Filter>     <!--Map Filter -    <filter-mapping>        <Filter-name>Filterdemo</Filter-name>        <!--"/*" means to intercept all requests -        <Url-pattern>/*</Url-pattern>    </filter-mapping>    <Dispatcher>REQUEST</Dispatcher></Web-app

third, the life cycle of filter

The creation and destruction of the filter is the responsibility of the Web server. When the Web application starts, the Web server creates an instance object of filter, invokes its Init method, completes the initialization of the object, and prepares the interceptor for subsequent user requests, and the filter object is created only once, and the Init method executes only once. The Filterconfig object representing the current filter configuration information can be obtained through the parameters of the Init method. The Web server calls the Destroy method to destroy the filter. The Destroy method executes only once in the life cycle of the filter.

When the Web server starts:

When visiting:

When the Web server shuts down:

The filter interface has three methods, and this three is a method associated with the life of the filter:

Init (Filter config)

Represents the Filter object initialization method that is executed when the filter object is created.

Parameters:

Config is an object that represents the configuration information for the filter object, and the internal encapsulation is the configuration information for that filter.

dofilter (ServletRequest request, servletresponse Response,filterchain chain)

represents the core method of filter execution filtering, and if a resource is already configured to filter on this filter, then each access to this resource will execute the Dofilter method.

filterchain:

The filter chain object, through which the Dofilter method of the object can release the request.

Destory ()

The representative is the filter destroy method that executes when the filter object is destroyed.

Java Web Learning Summary (a) filter filter

Related Article

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.