Servlet dynamic web technical explanation

  • 2020-05-30 20:56:03
  • OfStack

1. Introduction to Servlet:
With the gradual popularization of Internet technology, as well as the increasing requirements of people on the Internet, the former static web pages are no longer suitable, we see today's web pages not only have flash, vide and so on, obviously
To solve this problem, SUN provides a technology to solve the above problem, which is called Servlet technology.
Servlet is a technology provided by sun for developing dynamic web resources.
Sun provides an servlet interface in its API. Users who want to send a dynamic web resource (i.e., develop an Java program to output data to the browser) need to complete the following two steps:
Write one Java class to implement the servlet interface.
Deploy the developed Java class to the web server.

2. Operation process of Servlet:
The Servlet program is called by the WEB server. After the web server receives the Servlet access request from the client:
1. The Web server first checks to see if the Servlet instance object has been loaded and created. If so, proceed directly to step 4; otherwise, proceed to step 2.
2. Load and create one instance object of the Servlet.
3. Call the init() method of the Servlet instance object.
Create an HttpServletRequest object that encapsulates the HTTP request message and an HttpServletResponse object that represents the HTTP response message, then call the service() method of Servlet and pass in the request and response objects as parameters.
4. Before the WEB application is stopped or restarted, the Servlet engine (the class calling Servlet from the WEB server) unloads Servlet and calls the destroy() method of Servlet before uninstalling it.

3. Life cycle of Servlet:
1. In terms of life cycle, we have to mention the concept of cycle 1. What is the concept of life cycle?
Definition of life cycle: a thing, when it is born, when it dies, and the events that will be triggered at a certain point in its life cycle are collectively referred to as the life cycle of the thing.
The life cycle of Servlet:
Normally, the server will create an instance object of Servlet class (servlet born) when Servlet is called the first time. Once 1 is created, the Servlet instance will reside in memory for subsequent requests. The servlet instance object is not destroyed (servlet dies) until the web container exits.
During the entire life cycle of Servlet, the init method of Servlet is called only once when servlet is created.
Each access request to one Servlet causes the Servlet engine to call servlet's service method once. For each access request, the Servlet engine creates a new HttpServletRequest request object and a new HttpServletResponse response object,
The two objects are then passed as arguments to the service() method it calls on Servlet, which in turn calls the doXXX method on request. Before servlet is destroyed, the destroy() method is called.

2. Implementation class of Servlet interface:
We know that if we want to implement the Servlet interface, we have to implement all the methods inside, but all the methods inside are not what we want, so what is the use of implementing this method at this time?
So in order to solve this problem, we will not implement the interface, but will inherit the implementation class of this class, so we only need to implement the method we want;

2.1 SUN provides the commonly used implementation classes:
Servlet interface SUN defines two default implementation classes, GenericServlet and HttpServlet.

HttpServlet refers to servlet which can handle HTTP requests. It adds some protocol processing methods to the original Servlet interface, which is more powerful than Servlet interface. So when developers write Servlet, they should typically inherit from this class and avoid implementing the Servlet interface directly.

When HttpServlet implements Servlet interface, it overrides service method. The code in this method will automatically determine the request mode of the user. If the request is GET, doGet method of HttpServlet will be called; if the request is Post, doPost method will be called. So when developers write Servlet,
Usually you only need to override the doGet or doPost methods, not the service methods.

Since the client accesses the resources in the web server through the URL address, the Servlet program must map the servlet program to an URL address if it wants to be accessed by the outside world. This work is used in the web.xml file < servlet > Elements and < servlet-mapping > The element is complete.
< servlet > The element is used to register Servlet, which contains two main child elements: < servlet-name > and < servlet-class > , respectively, to set the registration name of Servlet and the full class name of Servlet.
1 < servlet-mapping > The element is used to map the external access path of a registered Servlet, which contains two child elements: < servlet-name > and < url-pattern > Is used to specify the registered name of Servlet and the external access path of Servlet, respectively. Such as:


2.2 small details of 1 in Servlet:
Detail 1:
The same Servlet can be mapped to multiple URL, that is, multiple < servlet-mapping > Elements of the < servlet-name > The setting value of the child element can be the same as the registered name of an Servlet.
The * wildcard can also be used in URL, which Servlet maps to, but only in two fixed formats: one that is "*. Extension" and one that begins with a forward slash (/) and ends with "/*".


Detail 2:
For the following mapping relationships:
Servlet1 maps to /abc/*
Servlet2 maps to /*
Servlet3 maps to /abc
Servlet4 maps to *.do
When the request URL is "/abc/ a.html", "/abc/*" and "/*" match, which servlet responds
The Servlet engine will call Servlet1.
When request URL is "/abc", both "/abc/*" and "/abc" match, which servlet responds
The Servlet engine will call Servlet3.
When request URL is "/abc/", both "/abc/*" and "*.do" match, which servlet responds
The Servlet engine will call Servlet1.
When request URL is "/", both "/*" and "*.do" match, which servlet response
The Servlet engine will call Servlet2.
When the request URL as ". / xxx yyy/a do ", and "/ *" "*. do" match, which servlet response
The Servlet engine will call Servlet2.
If the < servlet > One is configured in the element < load-on-startup > Element, then the WEB application will load and create the instance object of Servlet and call the init() method of the Servlet instance object.
For example:


Purpose: write one InitServlet for the web application, which is configured to load at startup to create the necessary database tables and data for the entire web application.

Detail 4: thread safety issues
When multiple clients concurrently access the same Servlet, the web server will create one thread for each client's access request and call the service method of Servlet from this thread. Therefore, if the same resource is accessed within the service method, it may cause thread safety problems.
If an Servlet implements the SingleThreadModel interface, the Servlet engine calls its service method in single-threaded mode.
No methods are defined in the SingleThreadModel interface, just add the declaration that implements the SingleThreadModel interface to the definition of the Servlet class.
For Servlet, which implements the SingleThreadModel interface, the Servlet engine still supports concurrent access by multiple threads to the Servlet by generating multiple Servlet instance objects, and each concurrent thread calls a separate Servlet instance object.
Implementing the SingleThreadModel interface doesn't really solve the thread safety problem of Servlet, because the Servlet engine creates multiple Servlet instance objects, while really solving the multithreaded security problem is the problem of one Servlet instance object being called by multiple threads at the same time.
In fact, in Servlet API 2.4, SingleThreadModel is already marked as Deprecated (obsolete).

4. Common objects in Servlet:
1. ServletConfig object
1.1 one or more can be used in the Servlet configuration file < init-param > The TAB configures 1 of the initialization parameters for servlet.
1.2 when servlet is configured with initialization parameters, the web container will automatically encapsulate these initialization parameters into ServletConfig objects when creating servlet instance objects.
The ServletConfig object is passed to servlet when the init method of servlet is called. In turn, the programmer can get the current servlet from the ServletConfig object
Initialization parameter information.
2. ServletContext object
2.1 when the WEB container is started, it creates a corresponding ServletContext object for each WEB application, which represents the current web application.
2.2 a reference to the ServletContext object is maintained in the ServletConfig object, and developers can obtain the ServletContext object through the ServletConfig.getServletContext method when writing servlet.
2.3 since all Servlet in one WEB application share the same ServletContext object, communication between Servlet objects can be realized through ServletContext objects.
ServletContext objects are also commonly referred to as context domain objects.

Related articles: