servlet / filter / listener / interceptor filter, the listener, the interceptor distinguish

  Because before been hard to tell the difference between filters and interceptors, it has been almost two illusion, so here summarize servlet / filter / listener / interceptor filter, listener, the interceptor.

 

Prior to briefly review under the servlet:

Overview: servlet is a server running java application that works in the client request and server response of the intermediate layer.

The main role: that interactively view and modify data, generate dynamic Web content.

Access to the process:

1, the client sends a request to the server; 
2, server request information to the Servlet; 
. 3, the Servlet response generated content and pass on to the server. In response to dynamically generated content, usually depends on the client's request; 
4, the server returns the response to the client.

 

filter:

Overview: filter is a multiplexed code snippets can be used to convert HTTP request and response headers. It can not produce a request or response, it is only for a resource modification request, or to modify the response from one.

The main purposes: encoding filter character, do some business logic judgment.

How it works: that is, as long as you have configured in the web.xml file to block a client request, it will help you intercept the request, then you will be able to request or response (Request, Response) set up a unified coding, streamline operations; at the same time also logical judgment, such as whether the user is logged in, there is no access to the page, and so on work. It is with your web application launch and boot, initialization only once, since you can intercept a related request only when your web application is stopped or redeployment was destroyed.

Access to the process:
1, before reaching the HttpServletRequest Servlet, intercept customers HttpServletRequest.
2. The need to check HttpServletRequest, may modify the header and data HttpServletRequest.
3, before reaching the client HttpServletResponse, interception HttpServletResponse.
4. The need to check HttpServletResponse, HttpServletResponse may modify the header and data.

Overall: Filter pretreatment user requests, then the request to the Servlet to process and generate a response, and then after the last treatment Filter server response.

 

listener:

Overview: As can be seen literally listener listening mainly used, can monitor a certain action performed by the web server listener, and respond accordingly based on their requirements. That is to say in plain language application, session, request the demise of the three objects created or modified to add functional components automatically execute code when deleting attributes. It also starts with the web application is started, initialization only once, stop with the web application and destroyed.

Main functions: add content to do some initialization work, set some basic elements, such as some parameters or some fixed object, and so on. For example, spring director will listen to instantiate the object of our bean configured in the server start.

 

interceptor :

First emphasized at: interceptor does not belong to the basic functions provided by the java web

Summary: It is a Java-based in reflection, is a form using Aspect Oriented Programming (AOP) for a prior method or field being accessed, intercept, and before or after the addition of certain operations. For example, to print before you call a method of a string (or do other business logic operation), you can also print out the string after you call the method, and even do business logic operation when you throw an exception.

Interceptor is invoked web frame based on the SpringMVC SpringMVC is dependent on the frame, you can use Spring dependency injection (DI) for a number of business operations, while an interceptor instance can be called multiple times in the life cycle of a controller, but it should be noted that some other such requests directly access static resources is no way to intercept processing.

 

The foregoing summary of the case:

servlet to provide basic web content;

Filter properties of some common functions between the server and the functions, dependent on the Servlet;

listener is to provide the foundation for dynamic interactive features of some of the operating environment

(Note: the servlet, filter, listener in web.xml load order is: context-param -> listener - > filter -> servlet) -> Barber order)

Aspect Oriented Programming interceptor, the nature of the program is to address the demand level, not the problem architecture level. It depends on the frame;

 Finally talk about the difference between the filter and the Interceptor:

Interceptor (Interceptor) is based on the Java reflection mechanism, and the filter (the Filter) callback function is based;

Filters can only be used before and after the request, the interceptor can details to each method.

Filter for almost all requests to work, but can only act on the action request Interceptor

 

Functionally speaking Filter is doing a thing for coding the URL, filter out useless parameters, safety check (such as log without logging the like)

而从灵活性上说拦截器功能更强大些,Filter能做的事情,都能做,而且可以在请求前,请求后执行,比较灵活;

 

Guess you like

Origin www.cnblogs.com/zzjlxy-225223/p/11263345.html
Recommended