The difference between filter and servlet

Comparison of filter and servlet

Mainly introduce the difference between them from the following four aspects:
                1. Concept.
                2. Life cycle.
                3. Responsibilities.

                4. Execution process.

 

    1. Concepts:
 
         1. Servlet: A servlet is a Java application running on the server side. It has the characteristics of being independent of platform and protocol, and can dynamically generate web pages. It works in the middle layer of client request and server response.
 
         2. Filter: A filter is a reusable code fragment that can be used to convert HTTP requests, responses, and header information. Unlike Servlet, Filter cannot generate a request or response. It only modifies a request for a certain resource or modifies a response from a certain resource.


       2. Life cycle:
 
        1. Servlet: The life cycle of a servlet begins when it is loaded into the memory of the web server and ends when the web server terminates or reloads the servlet. Once a servlet is loaded into the web server, it is generally not deleted from the web server's memory until the web server is shut down or ended again.
        (1) Load: Load an instance of Servlet when the server is started;
        (2) Initialize: Start when the web server is started or when the web server receives a request, or at some point in between. The init() method is responsible for the completion of the initialization work;
        (3), call: from the first access to the subsequent multiple visits, only call doGet() or doPost() method;
        (4), destroy: when the server is stopped Call the destroy() method to destroy the instance. 
 
        2, filter: (must implement the three methods init(), doFilter(), destroy() of the Filter interface of the javax.servlet package, empty implementation is also OK)
        (1), load the filter instance when starting the server, and Call the init() method to initialize the instance;
        (2) Only call the method doFilter() for processing each time a request;

        (3) Call the destroy() method when the server is stopped to destroy the instance.

 

 

  3. Responsibilities
 
       1. Servlet:
 
        create and return a complete html page containing dynamic content based on the nature of the customer request;
        create a part of the html page (html fragment) that can be embedded in the existing html page;
        read the client sent The hidden data;
        read the display data sent by the client;
        communicate with other server resources (including databases and java applications);
        send hidden data to the client through the status code and response header.
 
       2. Filter:
 
        Filter can preprocess the user request before a request reaches the servlet, or process the http response when leaving the servlet:
        before executing the servlet, first execute the filter program and do some preprocessing for it;
        modify it according to the program needs Request and response;
        intercept the execution of the servlet after the servlet is called


        Four, the difference:

      1. The servlet process is short. After the URL is sent, it will be processed, and then returned or redirected to a page specified by itself. It is mainly used to control before business processing.
        2. The filter process is linear. After the URL is passed, after checking, the original process can continue to be executed downwards, and received by the next filter, servlet, etc., and after the servlet is processed , Will not continue to pass down. The filter function can be used to keep the process going in the original way, or to lead the process, while the servlet function is mainly used to lead the process.
         filter can be used to filter character encoding, filter to detect whether the user is logged in, prohibit page caching, etc.

5. Implementation flow chart:

 

         1、servlet:

               

          2、filter:

           

 

 

There are listener and interceptor, the difference and connection of the four of them

 

http://blog.csdn.net/hy6688_/article/details/38666569

Guess you like

Origin blog.csdn.net/Amos_liu/article/details/73152597