Servlet interface implementation class

Servlet overview

  • The servlet specification comes from one of the javaEE specifications
  • effect:
    • In the servlet specification, specify the development steps of [dynamic resource file]
    • In the servlet specification, specify the http server to call dynamic resource file rules
    • In the servlet specification, specify the http server management dynamic resource file instance object rule

Servlet interface implementation class

  • The servlet interface comes from an interface under the servlet specification, which exists in the jar package provided by the http server.

  • There is a servlet-api.jar in the lib folder under the tomcat server to store the servlet interface (javax.servlet.servlet)

  • According to the servlet specification, the [dynamic resource file] that the http server can call must be a servlet interface implementation class

  • example:

    class Student(){
          
          
    //不是动态资源文件,tomcat无权调用
    }
    
    class Teacher implements Servlet(){
          
          
    //这是一个合法的动态资源文件,tomcat有权调动他
      servlet obj = new TEacher();
      obj.doget();
      }
    

Servlet interface implementation class development steps

  • Only as an implementation class of the servlet interface can it be qualified as a dynamic resource file
  • The servlet interface is provided by the http server manufacturer
  • tomcat/lib/servlet-api.jar servlet interface comes from this package

first step:

Create a java class, inherit the HttpServlet parent class (abstract class). Make it a Servlet interface implementation class.

Oneservlet extends HttpServlet implements Servlet

The four methods of init getservletconfig getservletinfo destroy in the servlet interface are useless for the servlet interface implementation class.

Only the service() method is useful.

Tomcat calls the servlet interface implementation class rules according to the servlet specification:

  1. Tomcat has the right to create instance objects of the servlet interface implementation class

servlet oneservlet = new oneservlet();

  1. Tomcat calls the service method according to the instance object to process the current request

oneservlet.service()

Child-parent relationship

oneservlet inheritance----->(abstract)httpservlet inheritance----->(abstract)Genericservlet implementation------->servlet interface

The four methods of init getservletconfig getservletinfo destroy in the servlet interface are all implemented in Genericservlet. The service() method is still an abstract method in the Genericservlet class

service() is implemented in oneservlet

The second step:

Override the two methods of the httpservlet parent class, doGet or doPost

According to the request method sent by the browser, override the doget or dopost method in the parent class to process the request.

The browser sends the request in the get mode------------->onservlet.doget()

The browser sends the request in post mode------------->oneservlet.dopost()

The invocation of doget and dopost is invoked through the service() method that is overridden in the httpservlet class. This in the httpservlet class represents the oneservlet class

image-20210211014607105

The parent class decides under what circumstances to call the method in the subclass, [design mode]-template design mode

httpservlet类:
service(){
		if(请求方式==post){
		this.dopost();
		}else if(请求方式==GET){
			this.doGet();
		}
}
onservlet类:
doGet  doPost

Supplement: this point to the problem

This points to the instance object that calls the service method.

Because it is oneservlet.service(), the this in this.doGet in service refers to oneservlet, and the method of execution is the rewritten doGet method in oneservlet.

If the doGet method is not overridden in oneservlet, this.doGet in oneservlet.service() executes the doGet of the parent class

third step:

[Registration] (notification) of the servlet interface implementation class information to the tomcat server

To the core configuration file (web.xml) of the website to register dynamic resource files with tomcat

[Website file]—[web]—[web-inf]—[web.xml]

image-20210211022845648

Pass the servlet interface implementation class path address to tomcat

<servlet>
<servlet-name>name</servlet-name> <!-- 声明一个变量储存servlet接口实现类的路径-->
<servlet-class>com.test.controller.Oneservlet</servlet-class> <!-- 声明servlet接口实现类的路径-->  
</servlet>

Tomcat:

String name = “com.test.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>name</servlet-name>
<url-pattern>/one</url-pattern><!--在此设置简短的请求别名。书写时以‘/’开头-->
</servlet-mapping>

If the browser now asks the tomcat server for the oneservlet address, write it as

http://localhost:8080/myweb/one

Guess you like

Origin blog.csdn.net/weixin_43903813/article/details/113798050
Recommended