Java Servlet technology

1. Introduction to Servlets

Servlet is one of the specifications of JavaEE. Generally speaking, it is a Java interface. In the future, we can define Java classes to implement this interface, and the Web server will run the Servlet, so TomCat is also called the Servlet container.

Servlet provides dynamic web resource development technology, a technology that can submit web page data to Java code, and return the data of Java program to web page. Using Servlet technology, different users can dynamically display different content on the page after logging in. function.

2. Life cycle

The life cycle of a Java Servlet refers to a series of processes that a Servlet instance goes through from creation to destruction. The life cycle of Java Servlet can be divided into the following three phases:

1. Initialization phase

The initialization phase is the phase that is executed immediately after the Servlet instance is created, and it will only be executed once. In this phase, the Servlet container will instantiate the Servlet class and call its init() method to complete the initialization work. The init() method can receive a ServletConfig object as a parameter to obtain the configuration information of the Servlet.

2. Service stage

When the Servlet container receives a request from the client, the container will create a ServletRequst object and a ServletResponse object for the request, pass them into the service() method as parameters, and call this method to process the request. The init() method must have executed successfully before the service() method can be executed.

In the service() method, the Servlet obtains the relevant information and request information of the client through the ServletRequst object. After the request processing is completed, the response information is packaged through the ServletResponse object and returned to the client. When the Servlet container returns the response information to the client, the ServletRequst object and the ServletResponse object will be destroyed.

During the entire life cycle of the Servlet, for each request of the Servlet, the Servlet container will call the service() method once, and create new ServletRequest and ServletResponse objects. That is, the service() method will be called multiple times during the entire life cycle of the Servlet.

3. Destruction stage

The destruction phase refers to a series of operations performed before the Servlet container will destroy the Servlet instance, and it will only be performed once. In this phase, the Servlet container will call the Servlet's destroy() method to release the resources occupied by the Servlet, such as closing the database connection, unregistering the listener, and so on.

@WebServlet(name = "LifeServlet",urlPatterns = "/life")
public class LifeServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("init 被执行了");
        super.init();
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("service被执行了");
        super.service(req, res);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doget被执行了");
    }

    @Override
    public void destroy() {
        System.out.println("destory被执行了");
        super.destroy();
    }
}

3. Configuration Mapping

The configuration and mapping of Java Servlet refers to associating the Servlet configuration information with the URL mapping of the Web application, so that the client can access the Servlet through the URL. Servlet configuration includes Servlet name, Servlet class name, Servlet parameters, etc. In a Java web application, servlets can be configured in two ways:

1. Use web.xml

web.xml is the configuration file for the web application and it is located under the WEB-INF directory. Servlets can be configured in the web.xml file. Example: Map a Servlet class named helloServlet to the URL "/hi"

<servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>net.zixue.servlet.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hi</url-pattern>
</servlet-mapping>

2. Use annotations

After servlet3.0, we don't need to configure servlet in web.xml , we only need to add @WebServlet annotation to modify the properties of the servlet. The servlet attributes that can be configured in web.xml can be configured in @WebServlet.

@WebServlet(name = "ServletTest1",urlPatterns = "/test1")
public class ServletTest1 extends HttpServlet {
    //...
}

Guess you like

Origin blog.csdn.net/mshxuyi/article/details/131379891