Javaweb Java Filter

Source: Internet
Author: User


1. Sometimes we need to do the same in each page of the program, such as login verification, request parameter encoding problem, and so on, then we need to write a filter to do these repetitive actions. The filter is the equivalent of when you want to go somewhere, you will definitely pass me (filter) This, and have to check if you have something wrong, and I will decide if you can go past (can be the filter in the code, that is Chain.dofilter (request,response ))。 Anyway, like a security guard in front of me.


2. The filter application is divided into two steps:

A) Writing filter

b) Configure the filter in Web. xml

3. Writing filter handwriting must implement the Javax.servlet.Filter interface, the method of initializing and destroying the object can be an empty method body, and the Dofilter method is to filter the content to be executed.

4. An instance of Helloword

Public class Hellofilter Implements filter{

// "Start execution, execute once after instance is created"

@Override

public void init (filterconfig filterconfig)throws servletexception {}

// Each access time execution, execution n times

@Override

public void doFilter (servletrequest request, Servletresponse Response,

Filterchain chain) throws IOException, servletexception {

System. out . println ("3. filter Business Processing operations ( common operation processing begins ) " );

// release ( go to servlet) to execute the requested resource

chain.dofilter (Request, response);

System. out . println ("5. End of execution " );

    }

@Override

public void destroy () {}

}

5. Flow of filter execution

Server Startup:

1. Call the filter construction method to create a filter instance

2. Call the Init method to execute the initialization method

User access:

3. Filter Filtering Method: DoFilter (Request,response,chain);

4. Handling the filter business

5. After processing, release (Chain.dofilter)

Next, go to the next filter, and execute the request resource (servlet or JSP) directly if there is no next filter

6. servlet Processing Business

7. Control goes back to the filter (Finally, the filter responds back to the browser)!

Server stops:

Executes the Destroy method for all filters.


6. Configure Filters

<!-- First a filter configuration -

<filter>

<filter-name>hellofilter</filter-name >

<filter-class>cn.itcast.a_hello. Hellofilter</filter-class>

</filter>

<!-- which requests to intercept ( now specifies blocking all requests :HTML/JSP/ Servlet) --

<filter-mapping>

<filter-name>hellofilter </ Filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

7. Detailed explanation of the filter configuration process

For the configuration of the filter, it should first be explained that the filter is a special servlet,filter configuration relatively simple imitation can be.

Filter-maping configuration is primarily how to configure filtering to a specified resource

Url-pattern the configuration

A) Url-pattern "/" represents the current project path

b) "/*" indicates that you want to filter all (directory) resources under the current Web project (including Css,js, pictures, and so on, which may cause errors in page display styles).

c) Specify the project directory, for example, just want to filter some of the pages I want to filter, you can directly write access to the path of the page. Example:/manage/home/main.jsp

D) You can also filter all JSP pages. Example: *.jsp

e) Of course, when Servlet-maping's url-pattern have. Action or. Do (similar to other suffixes) can also filter all servlets. Example: *.actioin or *.do (this is very common when filtering code)

Description:Url-pattern is not simply referring to the domain name address (location) in the browser

Instead, it refers to the URL of all requests made by the browser. such as (this is the Firefox Firebug tool, you can try):

Servlet-name Configuration

f) If you want to specify a servlet that needs to be filtered, you can configure Servlet-name in fiter-maping. The servlet-name here should be consistent with the configuration of the servlet-name in the servlet

< filter-mapping >

<filter-name>utf8encodingfilter </ Filter-name>

<!-- filtered directories or servlets --

<url-pattern>/manage/*</url-pattern >

<servlet-name>loginservlet </ Servlet-name>

<servlet-name>courseservlet</servlet-name >

<servlet-name>studentservlet </ Servlet-name>

<servlet-name>selecourseservlet </ Servlet-name>

<servlet-name>stuscoreservlet </ Servlet-name>

</filter-mapping>

The Url-pattern in the figure is to filter all the files in the Manage directory under the Web project directory.

Dispatcher Configuration

g) When the program executes to the specified request mode, it is filtered.

<dispatcher>REQUEST</dispatcher>

(By default, write the same effect without writing) direct access will be intercepted

<dispatcher>FORWARD</dispatcher> only intercepts forwarded requests

That is, execute Req.getrequestdispatcher ("index.jsp"). Forward (REQ,RESP) will be intercepted

<dispatcher>INCLUDE</dispatcher>

Execute Request.getrequestdispatcher ("/index.jsp"). Include (request, response);

, it will be intercepted.

<dispatcher>ERROR</dispatcher>

Filter interception when encountering a declarative exception, and then jump to the specified error page

Attached: Declarative exception:

<error-page>

<error-code>404</error-code>

<location>/xxjsp</location>

</error-page>

or

<error-page>

<exception-type>NullPo...</exception-type>

<location>/xx.jsp</location>

</error-page>

Javaweb Java 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.