Study notes (eight) Filter

Source: Internet
Author: User

1. Filter:

1). What is the Filter?

An important component of ①. Javaweb, which can intercept requests sent to the Servlet and intercept the response.
②. Filter is a Java class that implements the filter interface.
③. Filter needs to be configured and mapped in the Web. xml file.

2). How to create a Filter and run him up

①. Creating a Filter Class: Implementing the Filter Interface: public class Hellofilter implements Filter
②. Configure and map the Filter in the Web. xml file. where url-pattern specifies which resources the filter can intercept, that is, which URLs can be accessed to the filter

<!--registration Filter-
<filter>
<filter-name>helloFilter</filter-name>
<filter-class>com.atguigu.javaweb.HelloFilter</filter-class>
</filter>

<!--map Filter--
<filter-mapping>
<filter-name>helloFilter</filter-name>
<url-pattern>/test.jsp</url-pattern>
</filter-mapping>

3). Filter-related APIs:

①. Filter interface:

 > public void init (Filterconfig filterconfig): A servlet-like Init method. When you create a filter object (the filter object loads the current in the servlet container When the WEB app is created),
  is called immediately and is called only once. The method is used to initialize the current Filter. The Filter instance is a singleton.
 
  *  filterconfig similar to ServletConfig
  
  * can be used in Web. xml The initialization parameters of the current Filter are configured in the file. It is also configured in a similar way to the Servlet.
  
  <filter>
   <filter-name>hellofilter</ Filter-name>
   <filter-class>com.atguigu.javaweb.HelloFilter</filter-class>
   <init-param>
    <param-name>name</param-name>
    <param-value>root</param-value>
   </init-param
  </filter>

> public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain): The logic code of the real Filter needs to be written in this method. This method is called each time the interception occurs.

* Filterchain:filter chain. Multiple filter can form a filter chain.

-DoFilter (ServletRequest request, servletresponse response): Pass the request to the next filter in the filter chain,
If the current filter is the last filter in the filter chain, the request is given to the target Serlvet (or JSP)

-The order of multiple Filter interception is related to the order of <filter-mapping> configuration, before the first is called.

> public void Destroy (): The method that frees the resource that is being used by the current Filter. Called before the Filter is destroyed, and is called only once.

4). <dispatcher> element: Specifies how the resource intercepted by the filter is called by the Servlet container,
Can be one of Request,include,forward and error, the default request.
Multiple <dispatcher> child elements can be set to specify Filter to intercept multiple calls to a resource

①. REQUEST: When a user accesses a page directly, the Web container invokes the filter. If the target resource is accessed through the include () or forward () method of RequestDispatcher, then the filter is not called.

Direct access via GET or POST requests.

②. FORWARD: If the target resource is accessed through the RequestDispatcher FORWARD () method, then the filter will be called and the filter will not be called.

or <jsp:forward page= "/..."/> or forward pages through the ErrorPage of the page directive. <%@ page errorpage= "test.jsp"%>

②. INCLUDE: The filter will be called if the target resource is accessed through the RequestDispatcher INCLUDE () method. In addition, the filter is not called.

or <jsp:include file= "/..."/>

④. Error: If the target resource is called through the declarative exception handling mechanism, then the filter will be called. In addition, the filter is not called.

Declare through the Error-page node in the Web. xml file:

<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/test.jsp</location>
</error-page>

<filter-mapping>
<filter-name>secondFilter</filter-name>
<url-pattern>/test.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

Study notes (eight) Filter

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.