Servlet specification introduction

What is Servlet?

 Servlet (Servlet Applet), the full name is Java Servlert. It is a server-side program written in Java. Its main function is to interactively browse and modify data, and generate dynamic Web content . The narrow sense servlet refers to an interface implemented by the Java language, and the broad sense servlet refers to any class that implements this servlet. Under normal circumstances, people understand servlet as the latter. For example, the HttpServlet class inherits from the Servlet class, which can be used to implement Http requests by inheriting Http Servlet. When it is not an Http request, other forms of Servlet can also be defined.

        Servlet runs in a server that supports Java. In reality, servlet can respond to any type of request, but in most cases Servlet is only used to extend the Web server based on the THHP protocol.

        The first to support the Servlet standard is JavaSoft's Java Web Server. Since then, some other Java-based Web servers have begun to support standard Servlets.

       Servlet programming needs to use the classes and interfaces under the two packages javax.servlet and javax.servlet.http. Among all the classes and interfaces, the javax.servlet.servlet interface is the most important. All servlet programs must implement this interface and inherit classes that implement this interface. The following are the main classes and interfaces of Servlet:

        javax.servlet.ServletConfig;

        javax.servlet.ServletException;

        javax.servlet.HttpServlet;

        javax.servlet.HttpServletRequest;

        javax.servlet.HttpServletResponse;

        javax.servlet.HttpSession;

        javax.servlet.Cookie;

        javax.servlet.ServletContext;

        javax.servlet.GenericServlet;

The role of Servlet specification

1. Developed the development steps of [dynamic resource file]

2. Developed the rules of HTTP server call [dynamic resource file]

3. Established rules for managing instance objects of [dynamic resource files]

Servlet interface implementation class

    1. The Servlet interface comes from an interface under the Servlet specification, which exists in the jar package provided by the Http server

    2. There is a servlet-api.jar in the lib file under the Tomcat server to store the Servlet interface (javax.servlet.Servlet)

    3. In the Servlet specification, the [dynamic resource file] that the Http server can call must be an implementation class of the Servlet interface

Servlet interface implementation class development steps

    Step 1: Create a java class to inherit the HTTPServlet parent class and make it an implementation class of the Servlet interface

    Step 2: Rewrite the two methods of the HttpServlet parent class. doGet() or doPost()

        Browser--get--->oneServlet.doGet()

        Browser--post--->oneServlet.doPost()

    The third step: [register] the information of the Servlet interface implementation class to the Tomcat server. Registration: notification

        【Website】----->【web】------>【WEB-INf】------>wen.xml

        <!--Give the current Servlet interface implementation class path address to Tomcat-->

        <servlet>

            <servlet-name>path</servlet-name><!--Declare a variable to store the class path of the Servlet interface implementation class-->

            <servlet-class>school.xauat.controller.OneServlet</servlet-class><!--Declare the class path of the Servlet interface implementation class-->

        </servlet>

        Tomcat      String path="school.xauat.controller.OneServlet";

        <!--In order to reduce the difficulty for users to access the Servlet interface implementation class, it is necessary to set a short request alias -->

        <servlet-mapping>

            <servlet-name>path</servlet-name>

            <url-pattern>/one</url-pattern><!--Set a short request alias. When writing the alias, it must start with "/" -->

        </servlet-mapping>

 

        If the browser now asks Tomcat for the address of OneServlet

        http://localhost:8080/myWeb/one

/*
    Servlet规范中动态资源文件开发的步骤
 */
public class Class1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Class1.doGet() is running...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Class1.doPost() is running...");
    }
}

 

<?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>
        <servlet-name>Class1</servlet-name>
        <servlet-class>school.xauat.controller.review.Class1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Class1</servlet-name>
        <url-pattern>/class1</url-pattern>
    </servlet-mapping>
</web-app>

Established rules for managing instance objects of [dynamic resource files]

Servlet life cycle

    1. Instance objects of all Servlet interface implementation classes in the website can only be created by the http server

    Developers cannot manually create instance objects of the Servlet interface implementation class

    2. By default, when the http server receives the first request of the current Servlet interface implementation class

    Automatically create an instance object of this Servlet interface implementation class

 

    In the case of manual configuration, the http server is required to automatically create an instance object of the Servlet interface implementation class when it starts.

            <servlet>

                <servlet-name>path</servlet-name><!--Declare a variable to store the class path of the Servlet interface implementation class-->

                <servlet-class>school.xauat.controller.OneServlet</servlet-class><!--Declare the class path of the Servlet interface implementation class-->

                <load-on-startup>30</load-on-startup><!!--Fill in an integer greater than 0-->

            </servlet>

    3. During the running of the http server, only one instance object can be created for a Servlet interface implementation class

    4. When the http server is closed, all Servlet objects in the website are automatically destroyed

public class Class2 extends HttpServlet {
    public Class2(){
        System.out.println("Class2对象已经创建");
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Class2.doGet() is running...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Class2.doPost() is running...");
    }
}
 <servlet>
        <servlet-name>Class2</servlet-name>
        <servlet-class>school.xauat.controller.review.Class2</servlet-class>
        <load-on-startup>30</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Class2</servlet-name>
        <url-pattern>/class2</url-pattern>
    </servlet-mapping>

When the Tomcat server starts

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_45796208/article/details/108679348