Servlet study notes (1) (standard introduction, interface implementation class, development steps, life cycle)

One, Servlet specification introduction

1. Servlet specification comes from a kind of Java EE specification

2. Function:

(1) Specify the development steps of "dynamic resource files"

(2) Specify the rules for Http server to call "dynamic resource files"

(3) Specify the rules for Http server management "dynamic resource file" instance objects

Two, 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 folder under the Tomcat server to store the Servlet interface (javax.servlet.Servlet interface)

3. In the Servlet specification, the "dynamic resource file" that Http server can call must be a Servlet interface implementation class

//举例
class Student{
//不是动态资源文件,Tomcat无权调用
}

class Teacher implements Servlet{
//合法动态资源文件,Tomcat有权利调用
}

  Servlet obj=new Teacher();
  obj.doGet();

Three, Servlet interface implementation class development steps

Step 1: Create a new class oneServlet and inherit the parent class of HttpServlet to make it a Servlet interface implementation class

              Tomcat calls the Servlet interface implementation class according to the Servlet specification:
                         1. Tomcat has the right to call the instance object of the Servlet interface implementation class
                                   Servlet oneServlet=new Servlet();
                         2. Tomcat calls the service method according to the current instance object to process the current request
                                   oneServlet.service( );

                                           extends                                          extends                                             implements
                              oneServlet---------------->(abstract)HttpServlet--------------->(abstract)GenericServlet------------------>Servlet接口
                                                                                                                                              init
                                                                                                                                              getServletConfig
                                                                                                                                              getServletInfo
                                                                                                                                              Destory
                       here uses the
                       role of abstract classes:
                       reduce the difficulty of the interface implementation class to the interface implementation process, and let the abstract methods not used in the interface be handed over to the abstract class to complete, so that the interface implementation class only needs to use what it needs. The method can be rewritten.

Step 2: Rewrite the two methods of the HttpServlet parent class, namely doGet and doPost

               The parent class decides under what circumstances to call the method in the subclass, which uses a design pattern, the template pattern

HttpServlet : service(){
   if(请求方法==GET){
        this.doGet();
    }else if(请求方法==POST){
         this.doPost();
    }
}

The third step: "register" the information of the Servlet interface implementation class to the Tomcat server, and write it in web.xml

<!-- 将Servlet接口实现类的类路径交给Tomcat -->
<servlet>
   <servlet-name>oneServlet</servlet-name>  <!-- 声明一个变量存储 -->
   <servlet-class>com.mrliu22.oneServlet</servlet-class>  <!-- Servlet接口类的类路径,我的是com.mrliu22包下的oneServlet -->
<servlet/>

<!-- 为了降低用户访问Servlet接口实现类的难度,可以给其起个别名 -->
<servlet-mapping>
    <servlet-name>oneServlet</servlet-name>
    <url-pattern>/one</url-pattern>  <!-- 别名必须以“/”为开头 -->
</servlet-mapping>

Access path: http://localhost:8080/project name/one

Four, Servlet object life cycle

1. All instance objects of the Servlet interface implementation class in the website are created by the Http server, and developers cannot create them manually

2. By default, only when the Http server receives a request for the current Servlet interface implementation class, it will automatically create an instance object of the Servlet interface implementation class. You can also manually configure the Http server to create an instance object of a certain Servlet interface implementation class when it starts. You can configure it like this:

<servlet>
   <servlet-name>oneServlet</servlet-name>
   <servlet-class>com.mrliu22.oneServlet</servlet-class>
   <!-- 这里只要填写一个大于0的整数就可以手动设置Http服务器一启动就自动创建oneServlet接口实现类 -->
   <load-on-startup>30</load-on-startup>  
</servlet>

3. During the operation of the Http server, only one instance object of a Servlet interface implementation class will be created. It can be verified by printing the information in the construction method and then visiting multiple times.

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/MrLiu22/article/details/113030873
Recommended