For the understanding of Java Web Filter and the Interceptor.

Transfer product is slightly Library:  http://www.pinlue.com/article/2020/03/0316/559975739735.html

 

1.

Background in the design of web application, user login / registration is essential to the function, a method for verifying the user login information also varied, generally considered the following modes: front-end verification + background verification. According to the author's experience, usually some such as whether the input data format of the data entered is correct and a series of verification at the front, in the background will query the database for verification.

When general to verify in the background, you will choose to use the Filter Servlet as a blocker, Filter paper introduces Servlet, then let's compare it with Spring MVC's HnadlerInterceptor.

2. Filter

2.1 What is a Filter

Java Web Servlet as the basis for a comparison of its core has also been widely used feature is the Filter, also known as interceptors. As the name suggests, the interceptor is to play the role of interception. After Generally, a user makes a request to the server from the client, the entire process is:

HttpRequest ----> Filter ----> Servlet ----> Controller/Action/... ----> Filter ----> HttpResponse

According to the above process it can be seen that the role Filter is a user request arrives before the Servlet, to intercept. After intercepting the user's request, we can achieve some custom business logic, for example, when it comes to the login information will be verified before the user. Filter can be further modified in response to the data before it reaches the server in response to a client, the first paper describes a function.

2.2 Filter works of

Servlet Filter with the same server is responsible for creation and destruction, in the web application starts, the server calls the public void init (FilterConfig filterConfig) throws ServletException method to initialize Filter according to configuration information web.xml file for the application in, when the web application server is down or is removed, it will call public void destroy () to destroy Filter. A Filter will only be created in one application and once destroyed, after performing initialization finished, Filter declared a public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException methods used to implement some of the needs in after the completion of the business logic interception.

Noted above the doFilter () method parameter, this parameter has a chain, which chain is passed over the target intercept, which contains a series of user-defined interceptor, which is defined in accordance with interceptors in web.xml the order is executed. When the user authentication information or the current through the interceptor does not work, we can perform the chain.doFilter () method to skip the current interceptor to perform the next interceptor interceptor chain.

2.3 to achieve their own Filter

When achieve their own Filter, javax.servlet.Filter need to inherit interfaces and related methods.

2.3.1 The use of tools:

IDE: IntelliJ IDEA

Build tools: gradle

Local server: Tomcat

2.3.2 Specific Code

build.gradle

Published 60 original articles · won praise 52 · views 110 000 +

Guess you like

Origin blog.csdn.net/yihuliunian/article/details/104649760