JavaEE Servlet Technology

Tomcat is a servlet container that implements the servlet specification. It can run our own servlet applications to process dynamic requests and return responses. The following are some common object elements such as Listener, Filter, Servlet, Request, and Responses.

1. Listener

Listener (listener) is used to monitor the events of some objects. When the event occurs, it can enter the definition listener for processing. There are mainly three interfaces: ServletContextListener, HttpSessionListener and ServletRequestListener, which are defined as follows:

public interface ServletRequestListener extends EventListener {
    void requestDestroyed(ServletRequestEvent var1);
    void requestInitialized(ServletRequestEvent var1);
}

public interface ServletContextListener extends EventListener {
    void contextInitialized(ServletContextEvent var1);
    void contextDestroyed(ServletContextEvent var1);
}

public interface HttpSessionListener extends EventListener {   
    public void sessionCreated(HttpSessionEvent se);    
    public void sessionDestroyed(HttpSessionEvent se);    
}

Our program can implement the listener interface provided in these JavaEE Servlet API, and then define the deployment function through web.xml. For example:

 <!-- 配置log4j.xml监听器,注意该 listener需要放在spring的Listener之前 -->
 <listener>
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>

 <!--spring监听-->
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <listener>
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
 </listener>  

The servlet container needs to complete the instantiation of the listener classes in the web application before starting to execute the first request into the application , and keep a reference to each listener until the last request of the web application is served.

2. Filter

Filter (filter) can change the content, response, and header information of HTTP requests. Filters usually do not generate responses or respond to requests like servlets, but modify or adjust requests to resources, and modify or adjust responses from resources. .
The interface is defined as follows:

public interface Filter {
    void init(FilterConfig var1) throws ServletException;

    void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException;

    void destroy();
}

The init method is called when the container starts, and the destroy method is called when the container is destroyed. The above two methods are called twice in the entire life cycle.

When the container receives an incoming request, it will get all the filters in the list that match the request to form a filter chain (FilterChain), and then call the Filter.doFilter() method of each filter nested through the FilterChain (in the filter Call FilterChain.doFilter again, this is the chain of responsibility mode)

Filters are declared via the <filter> element in the deployment descriptor, and a filter or set of filters can be configured for invocation by defining <filter-mapping> in the deployment descriptor.

When the filter chain calls all the Filter.doFilter() methods, the related Servlet.service() methods are called and handed over to the Servlet (DispatherServelet in the Spring framework) for processing.
Note that Filter.doFilter() can prevent calls chained by the filter, and the filter is responsible for populating the Reponse object when it returns.

3. Servlet

Servlet here refers to the handler that implements the javax.servlet.Servlet interface. In general development, it inherits HttpServlet, which inherits from the GenericServlet class that implements the Servlet interface.

The container calls the Servlet.init() method when the web application is deployed, or when the servlet is processed for the first time; the destroy method of the servlet is called when the container is destroyed. When the FilterChain.doFilter ends, the Servlet.service method is called, in The service will be divided into doGet() and doPost() methods according to the request type.

4. Request与Response

The Request object encapsulates all the information requested by the client, and the Response object encapsulates all the information returned from the server to the client.

Each Request/Response object is only valid within the scope of the Servlet.service() method or the Filter.doFilter() method; in the case of asynchronous processing, the object remains valid until the AsyncContext.complete() method is called.

Except for the AsyncContext.startAsync() and AsyncContext.complete() methods of asynchronous requests, the implementation of Request/Response objects does not guarantee thread safety, and multi-threaded access needs to be processed synchronously.

4. web.xml

Describes information such as the servlets and other components that make up the application, as well as related initialization parameters.

The component loading order in web.xml is:
context-param -> listener -> filter -> servlet(同类则按编写顺序执行)。

Reference:
https://blog.csdn.net/tjiyu/article/details/54590259

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324765471&siteId=291194637