JavaEE Foundation (01): Servlet implementation, lifecycle execution

Source code for this article: GitHub. Click here || GitEE. Click here

Introduction to Servlet

Server-side programs written in Java have platform-independent and protocol-independent features. Their main functions are to interactively browse and generate data and generate dynamic Web content.Using the Servlet, you can collect user input from web forms, present records from databases or other sources, and dynamically create web pages.

2. Ways of implementation

1. Inherit HttpServlet

  • Introduction to API

Inherited from GenericServlet.implementations that follow the HTTP protocol, HttpServlet assumes the abstract template role from the design mode perspective, and the template method: the service () method.Basic method: doPost(), doGet(), etc.service() method process, omitting part of the judgment logic.This method calls one or more of the seven do methods to complete the response to client requests.These do methods need to be provided by specific subclasses of HttpServlet, and this API encapsulation is a typical template method pattern.

  • Code Case
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("Execution: doGet");
    }

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

2. Inherit GenericServlet

  • Introduction to API

Implementation class of Servlet interface and ServletConfig interface. An Abstract class. The service method is abstract.

  • Code Case
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("Execution: service");
    }
}

3. Implement Servlet Interface

  • Introduction to API

Servlet is an interface that contains several core methods: init, getServletConfig, service, getServletInfo, destroy.

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

3. Life cycle

  • Loading and instantiation

When the Servlet container starts or the client sends a request, the Servlet container looks for the existence of the Servlet instance and, if so, reads it directly in response to the request; if not, creates a Servlet instance (in singleton design mode).load-on-startup can configure the creation time sequence.

  • Initialization: init()

Once instantiated, the Servlet container invokes the init method once to initialize the current Servlet.

  • Service: service()

After initialization, the Servlet is ready to respond to requests.When a client request is received, the service() method is invoked to process the client request, and the service() method of the HttpServlet invokes different template methods based on different requests.

  • Destroy: destroy()

When the Servlet container closes, the Servlet instance is also destroyed.When the Tomcat service is shut down, you can see how the method works through log printing.

4. Operation 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 test.

  • Serlet-name: Servlet registration name.
  • servlet-class: Servlet full path class name.
  • serlvet-mapping: The same Servlet can be mapped to multiple URL s.
  • url-pattern: The mapping path accessed by the Servlet.

2. Thread pool operation

Observe log printing for the third Servlet implementation described above: Thread.currentThread().getId();.

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

It is not difficult to see here that Servlet s execute as thread pools.

5. Source code address

GitHub·address
https://github.com/cicadasmile/java-base-parent
GitEE·address
https://gitee.com/cicadasmile/java-base-parent

Keywords: Java github Tomcat xml

Added by nosmasu on Tue, 10 Dec 2019 06:14:41 +0200