Java Web -- servlet (learning notes)

catalogue

1. What is a servlet?

2. Working mode of Servlet

3. What are the main types of servlets

4. Create a Servlet class and configure it to the server

4.1 create a Servlet class and inherit HttpServlet (created manually)

4.2 configuring servlet s in Tomcat (must be configured in web.xml)

4.3 automatic creation and configuration

 5. Life cycle of Servlet

5.1 overview:

5.2 verification:

 6. Steps for using Servlet

1. Create and configure

2. Override the service () method in the parent class

3. In this method, the request transmitted by the form or other html elements is accepted through req and the required processing is done. The corresponding response is completed through resp, such as jumping to the page.

1. What is a servlet?

Servlet (Server Applet), the full name of Java Servlet, has no Chinese translation. Is a server-side program written in Java. Its main function is to interactively browse and modify data and generate dynamic Web content. Servlet in the narrow sense refers to an interface implemented by java language. Servlet in the broad sense refers to any class that implements the servlet interface. Generally, people understand servlet as the latter.

The Servlet runs in an application server that supports Java. In terms of implementation, servlets can respond to any type of request, but in most cases, servlets are only used to extend Web servers based on HTTP protocol.

2. Working mode of Servlet

  • The client sends a request to the server
  • The server starts and calls the Servlet, which generates the response content according to the client request and transmits it to the server
  • The server returns the response to the client

3. What are the main types of servlets

4. Create a Servlet class and configure it to the server

4.1 create a Servlet class and inherit HttpServlet (created manually)

public class MyServlet extends HttpServlet {
    
}

4.2 configuring servlet s in Tomcat (must be configured in web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        //Set the Servlet name to be configured
        <servlet-name>MyServlet</servlet-name>
        //Set and change the path of the Servlet
        <servlet-class>com.pgx.servlet.MyServlet</servlet-class>
    </servlet>
    //Set mapping
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
         //Set pathname when accessing
        <url-pattern>/servlet</url-pattern>
    </servlet-mapping>
</web-app>

4.3 automatic creation and configuration

 

 5. Life cycle of Servlet

5.1 overview:

Among them, init(), service(), destroy() is the method of Servlet life cycle. It represents the process of Servlet from "birth" to "work" and then to "death". The Servlet container (such as TomCat) will call these three methods according to the following rules:

1.init(), when the Servlet is requested for the first time, the Servlet container will start calling this method to initialize a Servlet object, but this method will not be called by the Servlet container in subsequent requests, just like people can only be "born" once. We can use the init () method to perform the corresponding initialization. When this method is called, the Servlet container will pass in a ServletConfig object to initialize the Servlet object.

2.service() method. Whenever a Servlet is requested, the Servlet container will call this method. Just like people, you need to constantly accept the boss's instructions and "work". In the first request, the Servlet container will first call the init() method to initialize a Servlet object, and then call its service() method to work. However, in subsequent requests, the Servlet container will only call the service method.

3. Destruction. When the Servlet is to be destroyed, the Servlet container will call this method. Just like people, they will die when the time comes. This happens when you uninstall the application or close the Servlet container. Generally, some cleanup code will be written in this method.


5.2 verification:

public class MyFirstServlrt implements Servlet {
 
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("Servlet Initializing");
    }
 
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
 
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //Specifically provide response methods to the customer service end. Generally, we will deal with them accordingly in the process of changing the method
        System.out.println("Servlet Providing services");
 
    }
 
    @Override
    public String getServletInfo() {
        return null;
    }
 
    @Override
    public void destroy() {
        System.out.println("Servlet Destroying");
    }
}      

Access the Servlet in the browser. During the first access, the console outputs the following information:

Then we refresh 3 times in the browser:

The information output from the console becomes as follows:

 

Next, we close the Servlet container:

 6. Steps for using Servlet

1. Create and configure

2. Override the service () method in the parent class

  protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }

3. In this method, the request transmitted by the form or other html elements is accepted through req and the required processing is done. The corresponding response is completed through resp, such as jumping to the page.

Tags: Java Web Development servlet server

Posted by vcarter on Fri, 20 May 2022 01:41:15 +0300