Learn Notes _ Filter Detail _2 (filter Javaweb One of the three main components)

Source: Internet
Author: User

Filter details 54 ways to intercept

Let's do a test, write a filter, specify the filter resource as b.jsp, and then we directly access b.jsp in the browser, and you'll find the filter executed!

However, when we request.getrequestdispathcer ("/b.jsp"). Forward (Request,response) in a.jsp, the filter is no longer executed! That is, by default, only direct access to the target resource executes the filter, and forward executes the target resource without executing the filter! (The jump does not perform f)

 Public classMyfilterextendsHTTPFilter { Public voidDoFilter (httpservletrequest request, httpservletresponse response, Filterchain chain)throwsIOException, servletexception {System.out.println ("Myfilter ...");    Chain.dofilter (request, response); }}      <filter> <filter-name>Myfilter</filter-name> <filter-class>Cn.itcast.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>Myfilter</filter-name> <url-pattern>/b.jsp</url-pattern> </filter-mapping>
<body> b.jsprequest. Getrequestdispatcher ("/b.jsp"). forward (Request, response); %> </body>

http://localhost:8080/filtertest/b.jsp When you access b.jsp directly, the filter content is executed;  

Http://localhost:8080/filtertest/a.jsp to a.jsp, but a.jsp will forward to b.jsp, then the filter will not be executed!

In fact, the filter has four kinds of interception mode! They are: REQUEST, FORWARD, INCLUDE, ERROR.

L REQUEST: executes the filter when accessing the target resource directly . Include: direct access in the Address bar, form submission, hyperlinks, redirection , as long as the address bar can see the path of the target resources, is the request;

L FORWARD: forward Access execution filter . including Requestdispatcher#forward () methods, <jsp:forward> tags are forwarded access;

L include: contains access execution filters. including Requestdispatcher#include () methods, <jsp:include> tags are included access;

L ERROR: When the target resource is configured as <error-page> in Web. XML, and there is an exception, the filter is executed when it is forwarded to the target resource.

You can add 0~n <dispatcher> child elements to the <filter-mapping> to illustrate how the current access is blocked.

<filter-mapping>

<filter-name>myfilter</filter-name>

<url-pattern>/b.jsp</url-pattern>

<dispatcher>REQUEST</dispatcher>[Cui 1]

<dispatcher>FORWARD</dispatcher>[Cui 2]

</filter-mapping>

    < Filter-mapping>

        <filter-name>myfilter </filter-name>

       <url-pattern>< Span style= "Background-color: #ff6600;" >/b.jsp </url-pattern>

    </filter-mapping>[Cui 3]  

<filter-mapping>

<filter-name>myfilter</filter-name>

<url-pattern>/b.jsp</url-pattern>

<dispatcher>FORWARD</dispatcher>[Cui 4]

</filter-mapping>

[Cui 1]b.jsp is the target resource, the filter is executed when the b.jsp is requested directly

[Cui 2] when forwarding to the B.jsp page, the filter will be executed ( This declaration can be forwarded to also execute the filter )

[Cui 3] When no interception method is given, the default is request

[Cui 4] when forwarding to the B.jsp page, the filter will be executed! since <dispatcher>FORWARD</dispatcher> has been given, there is no default request! so filtering is performed only when forwarding to b.jsp, and is not executed when forwarded to b.jsp b.jsp

In fact , the most commonly used is the request and forward two kinds of interception methods, and include and error are less useful! The include is better understood, we no longer give the code here, can be modified by forward way, to test themselves. The error mode is not easy to understand, the following is an example of how to intercept the error:

<filter-mapping>

<filter-name>myfilter</filter-name>

<url-pattern>/b.jsp</url-pattern>

<dispatcher>ERROR</dispatcher>[Cui 5]

</filter-mapping>

<error-page>

<error-code></error-code>

<location>/b.jsp</location>[Cui 6]

</error-page>

<body>

a.jsp

<%

if (true)

Throw New RuntimeException ("hehe ~"); [Cui 7]

%>

</body>

[Cui 5] intercept mode is error

[Cui 6] execute the B.JSP as 500 error page

[Cui 7] When the user accesses the a.jsp page will throw an exception, that is, 500!

The server will then forward to b.jsp, which will execute the filter before!

6 Application Scenarios for filters

Application Scenarios for filters:

Perform pre-processing before executing the target resources, such as setting up the encoding , which is usually released , but does some preparation before the target resource is executed; [C8]

L Pass conditions to determine whether the release, such as verifying whether the current user is logged in, or whether the user IP has been disabled;

• After the execution of the target resources, do some follow-up special processing, such as the data of the target resource output to process [C9];

[C8] almost all the sevlet need to write request.setcharacterendoing () can put it into a filter (Chinese garbled must be added, because the schema behind the shadow of some encoding method, Can only be declared in the filter)

[C9] Inbound intercept!

7 Setting the target resource

When you deploy filter in the Web. xml file, you can execute the target resource through "*":

<filter-mapping>

<filter-name>myfilter</filter-name>

<url-pattern>/*</url-pattern>[Cui Ten]

</filter-mapping>

This feature is exactly the same as a servlet! With this feature, we can execute the filter when the user accesses the sensitive resource, for example: <url-pattern>/admin/*<url-pattern>, and can put resources that all administrators can access to the/admin path. The user's identity can then be verified by a filter.

You can also specify a target resource for <filter-mapping> as a servlet, for example:

<servlet>

<servlet-name>Myservlet</servlet-name>

<servlet-class>cn.itcast.servlet.MyServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Myservlet</servlet-name>

<url-pattern>/abc</url-pattern>

</servlet-mapping>

<filter>

<filter-name>myfilter</filter-name>

<filter-class>cn.itcast.filter.MyFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>myfilter</filter-name>

<servlet-name>myservlet</servlet-name>[Cui one]

</filter-mapping>

When a user accesses Http://localhost:8080/filtertest/abc, a servlet with the name Myservlet is executed, and a filter is executed.

[Cui] means filtering all resources

[Cui one] here does not specify <URL-PATTERN>, but specifies <servlet-name>! Note that it is the same as the configuration name of a servlet!

Learn Notes _ Filter Detail _2 (filter Javaweb One of the three main components)

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.