In the working principle of Servlet containers, there are two Servlet containers. The first one is very simple, and the second one is written according to the first one. In order to make the first container as simple as possible, it is not complete. Some complicated Servlet containers include TOMCAT 4 and 5) are described in other chapters about TOMCAT running.
Both Servlet containers process simple Servlet and staticResource. You can use the PrimitiveServlet In the webroot/directory to test it. Complex servlets will exceed the capacity of these containers. You can learn how to create complex Servlet containers from the inside book of TOMCAT running.
The classes of both applications are encapsulated in the ex02.pyrmont package. You must be familiar with the javax. servlet. Servlet interface before understanding how an application works. This interface is introduced first. Next, we will introduce the specific content of the servlet container service servlet.
Javax. servlet. Servlet Interface
For Servlet programming, the following two classes and interfaces must be referenced: javax. servlet and javax. servlet. http. Among these classes and interfaces, javax. servlet. Servlet interfaces are particularly important. All servlets must implement this interface or inherit the classes that have implemented this interface.
The Servlet interface has five methods:
- public void init(ServletConfig config) throws ServletException
- public void service(ServletRequest request, ServletResponse response)
throws ServletException, java.io.IOException
- public void destroy()
- public ServletConfig getServletConfig()
- public java.lang.String getServletInfo()
The init, service, and destroy methods are Servlet lifecycle methods. After the Servlet class is instantiated, the container loads init to notify the servlet that it has entered the service column. The init method must be loaded, and the Servelt can receive and request. To load the database driver and initialize some values, the programmer can rewrite this method. In other cases, this method is generally null.
The service method is called by the Servlet container to allow the Servlet to respond to a request. The Servlet container passes the javax. servlet. ServletRequest object and javax. servlet. ServletResponse object. The ServletRequest object contains the HTTP request information of the client. The ServletResponse encapsulates the servlet response. You can write the code that requires servlet service and customer request.
Before deleting a Servlet instance from a service, the Servlet container calls the destroy method. When the servlet container is closed or the servlet container requires more memory, it is called. This method is called only when all threads in the servlet service method exit, or when it times out. After the servlet container calls the destroy method, it no longer calls the servlet service method. The destroy method gives the servlet the opportunity to clear all the waiting resources, such as memory, file processing, and threads ), to ensure that all the statuses in the memory are synchronized with the servlet's current status. Listing 2.1 contains the PrimitiveServlet code. This Servlet is very simple. You can use it to test the Servlet container application in this article.
The PrimitiveServlet class implements javax. servlet. Servlet and provides interfaces for five servlet methods. The method is also simple: each time the init, service, or destroy method is called, The servlet writes the method name to the control port. The service method also obtains the java. io. PrintWriter object from the ServletResponsec object and sends the string to the browser.
- import javax.servlet.*;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class PrimitiveServlet implements Servlet {
- public void init(ServletConfig config) throws ServletException {
- System.out.println("init");
- }
- public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
- System.out.println("from service");
- PrintWriter out = response.getWriter();
- out.println("Hello.Roses are red.");
- out.print("Violets are blue.");
- }
- public void destroy() {
- System.out.println("destroy");
- }
- public String getServletInfo() {
- return null;
- }
- public ServletConfig getServletConfig() {
- return null;
- }
- }
Now, let's look at Servlet programming from the servlet container perspective. A fully functional Servlet container will do the following for each Servlet's HTTP request:
When the Servlet is called for the first time, the Servlet class is loaded and Its init method is called only once)
In response to each request, construct a javax. servlet. ServletRequest and javax. servlet. ServletResponse instance.
Activate the servlet service method and pass the ServletRequest and ServletResponse objects.
When the servlet class is closed, call the destroy method of the Servlet and uninstall the servlet class.
What happens inside the Servlet container is much more complicated. This simple servlet container does not have sound functions. Therefore, it can only run very simple servelt and cannot call the init and destroy methods of servlet. However, the following actions are also understood as how the Servlet container works:
◆ Wait for the HTTP request.
◆ Construct ServletRequest and ServletResponse objects
If a staticResource is requested, the process method of the StaticResourceProcessor instance is activated and the ServletRequest and ServletResponse objects are passed.
If the request is a Servlet, load the class, activate its service method, and pass the ServletRequest and ServletResponse objects. Note: In this servlet container, this class is loaded whenever the servlet is requested.
In the first application, the Servlet container consists of six classes.
◆ HttpServer1
◆ Request
◆ Response
◆ StaticResourceProcessor
◆ ServletProcessor1
◆ Constants
As shown in the preceding application, the static main method of the program entry is the HttpServer class. This method creates an HttpServer instance and calls its await method. This method waits for the HTTP request, and then creates a request object and a response object, which is assigned to the StaticResourceProcessor instance or ServletProcessor instance based on whether the request is staticResource or servlet.
The Constants class contains static find WEB_ROOT, which is referenced from other classes. WEB_ROOT indicates the PrimitiveServlet location and the staticResource of Container service. The HttpServer1 instance waits for an HTTP request until it receives a shutdown command. The shutdown command is the same as the previous one. This is the working principle of Servlet containers. Based on this principle, many Servlet applications are executed and return the expected results.
- Security multithreading in Servlet containers
- Open-source Servlet container Jetty
- Relationship between Servlet APIs and JSP internal objects
- Differences between JSP and Servlet
- Analysis of five common Servlet Methods