Java EE Foundation (01): servlet implementation mode, life cycle execution process

1、 Introduction to Servlet

The server-side program written in Java is independent of the platform and protocol. Its main function is to interactively browse and generate data and generate dynamic web content. Using servlet, you can collect user input from web forms, render records from databases or other sources, and dynamically create web pages.

2、 Implementation mode

1. Inherit httpservlet

Inherited from genericservlet Comply with the HTTP protocol implementation. From the perspective of design pattern, httpservlet plays the role of abstract template, and the template method: the service () method plays the role. Basic methods: dopost(), doget(), etc. Service () method flow, omitting some judgment logic. This method calls one or more of the seven do methods to complete the response to the client request. These do methods need to be provided by specific subclasses of httpservlet. This API encapsulation is a typical template method pattern.

public class ServletOneImpl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("执行:doGet");
    }

    @Override
    protected void doPost(HttpServletRequest request,IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("执行:doPost");
    }
}

2. Inherit genericservlet

Implementation classes of servlet interface and ServletConfig interface An abstract class The service method is an abstract method.

public class ServletTwoImpl extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest,ServletResponse servletResponse)
            throws ServletException,IOException {
        HttpServletResponse response = (HttpServletResponse)servletResponse ;
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("执行:service");
    }
}

3. Implement servlet interface

Servlet is an interface, which contains several core methods such as init, getservletconfig, service, getservletinfo and destroy.

public class ServletThreeImpl implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        servletConfig.getServletName();
        System.out.println("init 被调用...");
    }
    @Override
    public void service(ServletRequest servletRequest,IOException {
        System.out.println("ThreadId:"+Thread.currentThread().getId());
        System.out.println("service 被调用...");
        HttpServletResponse response = (HttpServletResponse)servletResponse ;
        response.getWriter().print("Servlet.Life");
    }
    @Override
    public void destroy() {
        System.out.println("destroy 被调用...");
    }
    @Override
    public ServletConfig getServletConfig() {
        System.out.println("getServletConfig 被调用...");
        return null;
    }
    @Override
    public String getServletInfo() {
        System.out.println("getServletInfo 被调用...");
        return null;
    }
}

3、 Life cycle

When the servlet container starts or the client sends a request, the servlet container will find out whether the servlet instance exists. If so, it will directly read the instance to respond to the request; If it does not exist, create a servlet instance (belonging to singleton design mode). Load on startup can configure the creation timing.

After instantiation, the servlet container will call the init method once to initialize the current servlet.

After initialization, the servlet is ready to respond to the request. When receiving a client request, call the service () method to process the client request, and the service () method of httpservlet will call different template methods according to different requests.

When the servlet container is closed, the servlet instance is destroyed at any time. When you close the Tomcat service, you can see the execution of this method through log printing.

4、 Run configuration

1、web. XML configuration

<servlet>
    <servlet-name>servletOneImpl</servlet-name>
    <servlet-class>com.node01.servlet.impl.ServletOneImpl</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servletOneImpl</servlet-name>
    <url-pattern>/servletOneImpl</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>servletTwoImpl</servlet-name>
    <servlet-class>com.node01.servlet.impl.ServletTwoImpl</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servletTwoImpl</servlet-name>
    <url-pattern>/servletTwoImpl</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>servletThreeImpl</servlet-name>
    <servlet-class>com.node01.servlet.impl.ServletThreeImpl</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servletThreeImpl</servlet-name>
    <url-pattern>/servletThreeImpl</url-pattern>
</servlet-mapping>

Request: http://localhost:6003/servletOneImpl Testing.

2. Thread pool run

Observe the log printing of the third servlet implementation: thread currentThread(). getId());。

ThreadId:32
ThreadId:33
ThreadId:32
ThreadId:31
ThreadId:32

It is not difficult to find that servlets execute in the form of thread pools.

5、 Source code address

GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>