@[TOC]

1.1 summary of the Servlet

Servlet is a set of specifications provided by SUN corporation. It is simply called the Servlet specification, and it is also one of the JavaEE specifications. We can learn servlets through the API just like we learned Java basics. It is important to note that there is no Servlet specification in the JDK API, so JavaEE apis are required. The latest version of Oracle is JavaEE8. This website introduces some new features of JavaEE8. Of course, we can access the official API, learn and read the content.

Go to the official API website and find the Javax. servlet package in the upper left section, and find the servlet in the lower left section, as shown below:

Reading the API, we get the following information:

First: a Servlet is a Java applet that runs on a Web server

Second: it can be used to receive and respond to requests from clients

Third: to achieve Servlet functions, you can implement Servlet interface, GenericServlet or HttpServlet inheritance

Fourth: The Service method is executed on every request

Fifth: Servlets also support configuration

Please see the following figure for details:

1.2 introduction to the Servlet

1.2.1 Servlet writing steps

1.2.1.1 Coding steps

  • Step 1: preparation – create a JavaWeb project

  • Step 2: Write a generic class that inherits GenericServlet and overwrites the Service method

  • Step 3: Configure the Servlet in web.xml

1.2.1.2 test

Deploy the project in Tomcat, then access the Servlet in a browser

The following is successful.

1.2.2 Analysis of Servlet Execution process

We send the request through the browser, and the request first reaches the Tomcat server, which parses the request URL, and then finds our application in the list of deployed applications. Next, we look for the web. XML configuration file in our application, find the FirstServlet configuration in web. XML, execute the service method, and finally, FirstServlet responds to the client browser. The whole process is shown below:

Summary of execution process in one sentence:

Browser -- >Tomcat Server -- > Our application -- > Web.xml in application -- >FirstServlet -- > Response BrowserCopy the code

As you can see, the part in the compiler is just the part behind web.xml to the Servlet.

1.2.3 Servlet Class View

In the course of Tomcat and Http and in the introductory case, we have defined our own servlets, which are implemented by choosing to inherit GenericServlet. In the introduction to the API of servlets, It proposes that we can inherit HttpServlet in addition to GenericServlet, and by looking at the servlet class view, we see that GenericServlet has a subclass of HttpServlet. Also, there are parameters ServletRequest and ServletResponse in the service method, and their relationship is shown below:

1.2.4 Servlet writing method

1.2.4.1 Compilation Method (※)

Yangyongli.blog.csdn.net/article/det…

1.2.4.2 HttpServlet usage details

Step 1: Create a Servlet inheriting HttpServlet in the getting Started case project

Note: Do not override any methods, as shown below:

Step 2: Deploy the project and test the access

When we entered the access URL for ServletDemo2 in the address bar, we got an access error with the status code 405. The prompt message is: method not allowed.

Step 3: Analyze the cause

Using HttpServlet:

We inherit HttpServlet and need to override the doGet and doPost methods to receive GET and POST requests.

To achieve code reusability, we only need to provide specific functionality in one of the doGet or doPost methods, and the other method only needs to call the method that provides the functionality.