[JavaWeb] (7) filter, javaweb Filter

Source: Internet
Author: User

[JavaWeb] (7) filter, javaweb Filter
1. Introduction

Definition: a filter is a server component that intercepts user-side request and Response Information and filters the information.

Lifecycle:


The instantiation is executed only once when the Web project is started; Initialization is also executed only once; filtering is executed multiple times, and each request is accepted once; destroy is executed when the Web container is closed.


2. Implement Filters

The Filter implements the javax. servlet. Filter interface. We need to implement three methods:

Init (): This is the initialization method of the filter. This method is called after the Web Container creates a filter instance. This method can read the filter parameters in the web. xml file.

DoFilter (): This method completes the actual filtering operation. This is the core method of the filter. When a user requests to access the URL associated with the filter, the Web Container first calls the doFilter method of the filter. The FilterChain parameter can call the chain. doFilter method to send requests to the next filter (or target resource), or forward requests to other resources by means of forwarding or redirection.

Destroy (): The Web Container calls this method before destroying the filter instance. In this method, the resources occupied by the filter can be released. (Not available in most cases)

Web. xml configuration:


<Filter> only one tag can be configured, and multiple <filter-mapping> tags can be configured.

A simple Web. xml configuration:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>    <filter>        <filter-name>FirstFilter</filter-name>        <filter-class>com.thr.filter.FirstFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>FirstFilter</filter-name>        <url-pattern>/index.jsp</url-pattern>        <dispatcher>REQUEST</dispatcher>    </filter-mapping></web-app>
The filter can change the Web resource requested by the user, that is, the path of the user request.

The filter cannot directly return data or directly process user requests.


3. when multiple filters in the filter chain filter a page at the same time, they are stored on the Web. the configuration sequence in xml executes the starting part of doFilter successively, then performs doFilter processing, and then executes the end part of doFilter in reverse order.


4. Filter classification in Servlet2.5:

(1). REQUEST: when the user accesses the page directly, the Web Container will call the filter. If not configured, the default REQUEST

(2). FORWARD: this filter is called when the target resource is accessed through the forward of RequestDIspatcher.

(3). INCLUDE: when the target resource is accessed through the include function of RequestDIspatcher, the filter will be called.

(4). ERROR: when the target resource is called through the declarative Exception Handling Mechanism, the filter will be called.

When redirection and REQUEST are used, an endless loop may occur.

When requests are forwarded and FORWARD is used, an endless loop may occur.

Added ASYNC in Servlet3.0: supports asynchronous processing.

@ WebFilter: declares a class as a filter. The annotation will be processed by the container during deployment, and the container will deploy the corresponding class as a filter according to the specific attribute configuration.

With @ WebFilter, you can configure the filter without configuring it in Web. xml.


@ Common attributes of WebFilter:


You can configure the FilterConfig initialization parameters in Web. xml, and get the initialization parameters through getInitParameter in the filter.


5. application scenarios

(1). perform unified authentication on user requests

(2). encoding conversion

(3). Filter and replace the data sent by the user

(4). Convert the image format

(5). compress the response content

One way to solve Chinese garbled characters is to set request. setCharacterEncoding ("UTF-8") in the filter.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.