Spring Boot Reference Tutorial (vi) Spring boot configuration Servlet,filter,listener,interceptor

Source: Internet
Author: User

4.2.5. Spring Boot Configuration Servlet,filter,listener,interceptor1. Configuring the Servlet

Some scenarios in development often use servlets, such as user logout . The following shows how to configure .

There are two ways to configure the use of Servlets, one to use annotations, and one to use a configuration class.

There are two ways to configure a servlet:

(1) annotation configuration Servlet

When you write a simple servlet that accesses this servlet, it jumps to the home page and adds an annotation @WebServlet (value = "/configservlet") to the servlet:

add annotations on the application class @ServletComponentScan

Post-launch access test:

Access:

Access results:

Jump Success!

(2) Code configuration Servlet

Add the following code after annotating the annotations with the servlet code above:

Test success!

2. Configure the filter

There are two configuration options for configuring a filter like configuration servlet:

(1) annotation configuration Filter

Write a filter to add an annotation @WebFilter ("/*"):

add annotations on the application class @ServletComponentScan

Start Access:

Access results:

Note: For a more detailed configuration of filter, you can view the configuration properties in note @webfilter ("/*"), such as configuring filter order of filters.

(2) Code configuration Filter

Note Write the configuration code after the annotations above:

Note: Filterregistrationbean. setorder (0); method to set the filter's order in the filter chain.

No more testing!

3. Configure Listener

There are two ways to configure listener with the Servlet, Fiter:

(1) annotation configuration Listener

The code is as follows:

Note: the optional listener for custom configuration listener is like the following table:

add annotations to the Application class:@ServletComponentScan

To start the test:

(2) Code configuration Listener

Note the above annotations and write the code:

No more testing!

4. Configure Interceptor

To write an interceptor:

To write a configuration class:

Test:

To access any available request:

< instance code >

Github:https://github.com/chunyuding/springboot-demo

Https://github.com/chunyuding/SpringBoot-Demo.git

Code Cloud: Https://gitee.com/dingchunyu/SpringBoot-Demo

Https://gitee.com/dingchunyu/SpringBoot-Demo.git

< recommended books >

Baidu Cloud: Http://pan.baidu.com/s/1qYA0Nxi

Spring Boot Reference Tutorial (vi) Spring boot configuration Servlet,filter,listener,interceptor

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.