The first Servlet implementation

Servlets are short for Server and Applet and stand for server-side applets. The use of Java language written server side procedures, can be like the generation of dynamic WEB pages, Servlet mainly run on the server side, and by the server call execution, is a class developed in accordance with the Servlet standard. Is a technology provided by SUN corporation for developing dynamic Web resources. (Translation: To implement Web development, you need to implement the Servlet standard.)

Servlets are Java classes in nature, but they are written in accordance with the Servlet specification. There is no main() method, and their creation, use, and destruction are managed by the Servlet container (such as Tomcat). Write your own class, don’t write main method, others automatically call)

Servlets are closely related to the HTTP protocol and can handle all content related to the HTTP protocol. This is one of the reasons servlets are so popular.

Servers that provide Servlet functionality are called Servlet containers, and there are many common containers, such as Tomcat, Jetty, WebLogic Server, WebSphere, JBoss, and so on.

1. The implementation

1) Create dynamic Web projects


2) Create a new class

3) Implement the Servlet specification

Implement the Servlet specification, that is, inherit the HttpServlet class, and into a package such as the response, the class has completed the rules of communication, we just need to carry out the implementation of the business.

4) Override the service method

The Servlet specification only allows our class to meet the requirements of receiving the request. After receiving the request, we need to analyze the request and perform business logic processing, and calculate the result, then we need to add code. In the specification, there is a method called Service, which is specially used to handle the request. Business code can be written in this method.

5) Configure web.xml

Once you’ve written all the code, you need to explain to the server that a particular request corresponds to a particular resource, in this case through a configuration file called web.xml.

6) Release the project and start the service

At this point, everything that needs to be written and configured is done, and the project is complete, but you still need to publish the project to the server and run it if you want it to be accessible to the outside world.

7) Start the project

Seeing something like a long list of messages indicates a successful startup, and you can access the project

8) Access and view the results

After the project is properly published to the server, the user can access the resources in the project through a browser. Note That the URL format is correct and the Tomcat port is 8080. http://localhost:8080/hw/helloweb, the results page

The background results

Here we have our first Servlet implemented!

1. Working principle

Implementation process

According to the configuration of the web. XML file, the client sends a request, finds the corresponding value in the read, finds the corresponding class, loads and executes the class, and returns the result. The Web server sends the result back to the client

2. Servlet life cycle

The Servlet does not have a main() method and cannot run independently. Its execution is completely controlled and scheduled by the Servlet engine. The life cycle refers to the entire process of when the servlet container creates a servlet instance, calls its methods for request processing, and destroys instances. (The default life cycle is discussed here)

L Instances and initialization times

When the request arrives, the container looks for the servlet object to see if it exists, and if it doesn’t, it creates an instance and initializes it.

L Ready/call/service phase

When a request arrives, the container calls the servlet object’s service() method, which can be called multiple times throughout the declaration cycle.

The HttpServlet service() method calls the doGet() or doPost() methods depending on the request. However, both do methods, by default, throw exceptions that require subclasses to override.

L Destruction time

When the container is closed (when the application is stopped), the Servlet instance in the application is destroyed.

The above lifecycle can be observed using the lifecycle method in the Servlet. There are three life-cycle methods in a Servlet, which are not called manually by the user but automatically called by the container at specific times. The life cycle of a Servlet can be observed by observing these three life-cycle methods.

Init method, executed after the Servlet instance has been created (to prove that the Servlet instance has been created)

The service method, executed each time a request reaches a Servlet method, to process the request (proving that the Servlet is serviced)

Method destroy, executed when the Servlet instance is destroyed (to prove that the instance of the Servlet was destroyed)

A simple summary of the Servlet life cycle can be divided into four steps: Servlet class loading > instantiation > service > destruction. Let’s describe how Tomcat works with servlets by looking at the sequence diagram below.

1. The Web Client sends an Http request to the Servlet container (Tomcat)

2. The Servlet container receives requests from the Web Client

3. The Servlet container creates an HttpRequest object and encapsulates the Web Client request information into this object

The Servlet container creates an HttpResponse object

5. The Servlet container calls the Service method of the HttpServlet object, passing the HttpRequest object and the HttpResponse object as parameters to the HttpServlet object

6, HttpServlet calls the relevant methods of the HttpRequest object to obtain Http request information

7. The HttpServlet calls the relevant methods of the HttpResponse object to generate the response data

The Servlet container passes the HttpServlet response to the Web Client

3. Configure servlets

In addition to configuring basic access information, servlets can also configure initialization parameters, self-start, etc., and a Servlet can configure multiple access paths (), and can also use the wildcard “*”.

The basic configuration

Initial parameters

Since the launch

Servlet-name: specifies the name of the servlet object

Servlet-class: The class to call to create the servlet object

Param-name: indicates the parameter name

Param-value: indicates the parameter value

Load-on-startup: The order in which Servlet objects are loaded when the Servlet container is started

Servlet-mapping /servlet-name: corresponds to the servlet-name configuration section in the servlet

Url-pattern: indicates the relative URL path of servlets accessed by customers

Description: Multiple URl-patterns can be configured (a servlet can be accessed by multiple URl-patterns). When multiple servlets are configured with the same URl-pattern, Error Java. Lang. Reflect. InvocationTargetExceptionion wildcard “*” can only be put in the front or the back, can’t put in the middle and cannot exist alone (with/division, and words of overall). The more precise the better. The general configuration is ok.

Thanks for reading this article on Lebyte technology. Stay tuned for more articles on Java technology :⑧⑥⑧⑤ zero ④ zero ⑨⑨. The next article will give you a detailed introduction to Web requests and common objects.