1. Introduction to Servlet:
With the gradual popularization of Internet technology and the increasing demands for the Internet, the static web pages used to be no longer adaptable. We see that today's web pages not only include flash, video, etc., obviously
The static web display in the past cannot be solved. In order to solve this problem, SUN company provides a technology to solve the problems mentioned above, which is Servlet technology.
Servlet is a technology provided by Sun Company for developing dynamic web resources.
Sun provides a servlet interface in its API. If users want to send a dynamic web resource (that is, develop a Java program to output data to the browser), they need to complete the following two steps:
Write a Java class to implement the servlet interface.
Deploy the developed Java classes to the web server.
2. The operation process of servlet:
The Servlet program is called by the WEB server. After the web server receives the client's Servlet access request:
The server first checks whether the instance object of the Servlet has been loaded and created. If yes, then execute step 4 directly, otherwise, execute step 2.
2. Load and create an instance object of the Servlet.
3. Call the init() method of the Servlet instance object.
Create an HttpServletRequest object for encapsulating HTTP request messages and an HttpServletResponse object representing HTTP response messages, and then call the Servlet's service() method and pass the request and response objects as parameters.
Before the application is stopped or restarted, the Servlet engine (the class that calls the Servlet in the WEB server) will uninstall the Servlet and call the destroy() method of the Servlet before uninstalling.
3. The life cycle of servlet:
1. Speaking of life cycles, we have to mention the concept of cycles. So what is the concept of life cycles?
Life cycle definition: An event that will be triggered at a certain point in its survival stage is collectively called the life cycle of that thing.
Servlet life cycle:
Normally, the server will create an instance object of the Servlet class when the Servlet is called for the first time (the servlet is born); once it is created, the Servlet instance will reside in memory to serve subsequent requests; until the web container exits, the servlet instance object will be destroyed (the servlet dies).
During the entire life of a servlet, the init method of the servlet is called only once when the servlet is created.
Each access request to a servlet causes the servlet engine to call the servlet service method once. For each access request, the Servlet engine creates a new HttpServletRequest request object and a new HttpServletResponse response object.
Then pass these two objects as parameters to the service() method of the Servlet it calls, and the service method then calls the doXXX method according to the request method. The destroy() method will be called before the servlet is destroyed.
Interface implementation class:
We know that if we want to implement the Servlet interface, we must implement all the methods in it. However, all the methods in it are not what we want. So, what's the use of implementing this method at this time?
So in order to solve this problem, we generally do not implement the interface, but inherit the implementation class of this class, so that we only need to implement the method we want;
2.1SUN company provides the commonly used implementation classes:
Servlet interface SUN company defines two default implementation classes, namely: GenericServlet and HttpServlet.
HttpServlet refers to a servlet that can handle HTTP requests. It adds some HTTP protocol processing methods to the original servlet interface, which is more powerful than the servlet interface. Therefore, when writing Servlets, developers should usually inherit this class and avoid directly implementing the Servlet interface.
When HttpServlet implements the Servlet interface, it overwrites the service method. The code in the method body will automatically determine the user's request method. If it is a GET request, the doGet method of HttpServlet is called. If it is a Post request, the doPost method will be called. Therefore, when developers write Servlets,
Usually you only need to override the doGet or doPost method instead of overwriting the service method.
Since the client accesses resources in the web server through a URL address, if the servlet program wants to be accessed by the outside world, it must map the servlet program to a URL address. This work is done in the file using the <servlet> element and <servlet-mapping> element.
The <servlet> element is used to register a servlet. It contains two main child elements: <servlet-name> and <servlet-class>, which are used to set the registration name of the servlet and the full class name of the servlet respectively.
A <servlet-mapping> element is used to map an external access path to a registered Servlet. It contains two child elements: <servlet-name> and <url-pattern>, which are used to specify the registration name of the Servlet and the external access path of the Servlet. For example:
<servlet>
<servlet-name>servlet3</servlet-name>
<servlet-class>.Demo3Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
Some small details in 2.2Servlet:
Detail 1:
The same Servlet can be mapped to multiple URLs, that is, the setting value of the <servlet-name> child element of multiple <servlet-mapping> elements can be the registration name of the same Servlet.
The * wildcard can also be used in the URLs to which the servlet maps, but there can only be two fixed formats: one is the "*. extension", and the other is the starting with a forward slash (/) and ending with a "/*".
<servlet-mapping>
<servlet-name>
AnyName
</servlet-name>
<url-pattern>
*.do
</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>
AnyName
</servlet-name>
<url-pattern>
/action/*
</url-pattern>
</servlet-mapping>
Detail 2:
For some mapping relationships below:
Servlet1 maps to /abc/*
Servlet2 maps to /*
Servlet3 maps to /abc
Servlet4 maps to *.do
question:
When the request URL is "/abc/", "/abc/*" and "/*" both match, which servlet responds
The Servlet engine will call Servlet1.
When the request URL is "/abc", both "/abc/*" and "/abc" match, which servlet responds
The Servlet engine will call Servlet3.
When the request URL is "/abc/", both "/abc/*" and "*.do" match, which servlet responds
The Servlet engine will call Servlet1.
When the request URL is "/", both "/*" and "*.do" match, which servlet responds
The Servlet engine will call Servlet2.
When the request URL is "/xxx/yyy/", both "/*" and "*.do" match, which servlet responds
The Servlet engine will call Servlet2.
Detail 3:
If a <load-on-startup> element is configured in the <servlet> element, then when the WEB application starts, it will load and create the Servlet instance object and call the init() method of the Servlet instance object.
For example:
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
Purpose: Write an InitServlet for the web application. This servlet is configured to load on startup to create necessary database tables and data for the entire web application.
Detail 4: Thread Safety Issues
When multiple clients access the same Servlet concurrently, the web server will create a thread for each client's access request and call the Servlet's service method on this thread. Therefore, if the same resource is accessed in the service method, it may cause thread safety problems.
If a Servlet implements the SingleThreadModel interface, the Servlet engine will call its service method in single-thread mode.
No method is defined in the SingleThreadModel interface, just add a declaration to the definition of the Servlet class to implement the SingleThreadModel interface.
For servlets that implement the SingleThreadModel interface, the Servlet engine still supports multi-thread concurrent access to the servlet. The method is to generate multiple Servlet instance objects, and each concurrent thread calls an independent Servlet instance object separately.
Implementing the SingleThreadModel interface cannot really solve the thread safety problem of Servlets, because the Servlet engine will create multiple Servlet instance objects, and truly solving the multi-thread safety problem refers to the problem that a Servlet instance object is called by multiple threads at the same time.
In fact, in Servlet API 2.4, SingleThreadModel has been marked as Deprecated (outdated).
4. Commonly used objects in Servlets:
Object
1.1 In the servlet configuration file, you can use one or more <init-param> tags to configure some initialization parameters for the servlet.
1.2 When the servlet has configured initialization parameters, when the web container creates servlet instance objects, it will automatically encapsulate these initialization parameters into the ServletConfig object.
And when calling the init method of the servlet, pass the ServletConfig object to the servlet. Furthermore, programmers can obtain the current servlet through the ServletConfig object.
Initialization parameter information.
Object
2.1 When the WEB container is started, it will create a corresponding ServletContext object for each WEB application, which represents the current web application.
2.2 The ServletConfig object maintains a reference to the ServletContext object. When writing a servlet, developers can obtain the ServletContext object through methods.
2.3 Since all Servlets in a WEB application share the same ServletContext object, communication can be achieved between Servlet objects through ServletContext objects.
ServletContext objects are usually also called context domain objects.