The students who have done the Java Web are very familiar with Tomcat. Are we thinking about how Tomcat works while using the convenience of Tomcat? Tomcat is essentially an HTTP server, and this article will build a simple HTTP server.
1 Catalina Model
First, let's look at the general workings of Tomcat. The core of Tomcat is the servlet container, which we call Catalina (why is this name?). I don't know  ̄へ ̄). Model diagram such as 1.1
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/912007/201606/912007-20160601140306086-823686505. PNG "width=" 597 "height=" 143 "style=" margin:0px;padding:0px;border:none;line-height:1.5; "/>
Figure 1.1
The connector is used to "connect" the container inside the request. Its job is to construct a request and response object for receiving every HTTP request. It then passes the process to the container. The container calls the Servlet's service method for the response after it receives the Requset and response objects from the connector. Remember, this description is just the tip of the iceberg. The container here has done quite a lot of things. For example, before it invokes the Servlet's service method, it must load the servlet, validate the user (if needed), update the user session, and so on. With this in mind, let's start our journey of constructing an HTTP server.
2 Server Setup
First we define the function point of our server.
1. A class is required to receive HTTP requests;
2. A custom request class and a response class are required to construct the received request into these two classes;
3. Determine the processing method according to the requested format: return static resource or enter the servlet?
4. Requires a servlet class to execute business logic
The UML diagram is as follows 2.1
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/912007/201606/912007-20160601154954649-414444686. PNG "width=" 709 "height=" 368 "style=" margin:0px;padding:0px;border:none; "/>
Figure 2.1
2.1 Httpserver
First, construct the Httpserver class
650) this.width=650; "id=" code_img_closed_4895baf5-2184-47af-9202-17b0f804df82 "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:none;vertical-align:middle; "/> View Code
Our server startup portal was placed inside the httpserver. The await () method is responsible for receiving the socket connection, and the server stops running only if the user enters a URL that represents shutdown. The request class provides the ability to parse the request, depending on the URL requested, to decide whether to return a static resource, or to enter the corresponding servlet class to execute the service logic.
Here we need to look at the use of the ServerSocket class. The socket class represents a client socket, that is, whenever you want to connect to a remote server application, you will think of this class for the first time. Unlike ServerSocket and sockets, the role of the server socket is to wait for a connection request from the client. Once the server socket obtains a connection request, it creates a socket instance to communicate with the client. One of the constructors for the servletsocket socket is
public ServerSocket (int port, int backLog, inetaddress bindingaddress);
Port represents the port number, and the backlog represents the maximum number of connections that the socket can support, and bindingaddress represents the address that the server binds to. Once you have a ServerSocket instance, you can call the ServerSocket class by calling the Accept method J. This listens for requests on the current port of the current address, and the method only returns when there is a connection request, and the return value is an instance of the Socket class.
2.2 Request Response
The servlet service method receives a Javax.servlet.ServletRequest instance and a javax.servlet.ServletResponse from the servlet container Instance. This means that for each HTTP request, the servlet container must construct a ServletRequest object and a Servletresponse object and pass them to the service method of the servlet that is serving.
650) this.width=650; "id=" code_img_closed_0b0434ab-231b-4e52-9363-7d6761863237 "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:none;vertical-align:middle; "/> View Code
the request class represents a request object and is passed to the Servlet's service method. In itself, it must implement the Javax.servlet.ServletRequest interface. This class must provide implementations of all methods of this interface. However, we want to make it very simple and provide only the ability to implement some of these methods, such as parsing URLs. The member variable InputStream is initialized at request initialization, and a byte array is created with the parseURL () method to read into the input stream and converted into a StringBuffer object to parse the URL.
650) this.width=650; "id=" Code_img_closed_2a8346c0-700c-408d-a802-fcf2580925a4 "class=" code_img_closed "src="/ Img/jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:none;vertical-align:middle; "/> View Code
The response class provides the ability to send static resources. The Sendstaticresource () method finds the specified file locally based on the URL parsed within the request. If found, the contents of the file read out to the browser, if not found, then return 404 error code.
2.3 Primitiveservlet class
650) this.width=650; "id=" code_img_closed_476d34fc-b17c-4386-9435-9765c3a7a39b "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:none;vertical-align:middle; "/> View Code
the Primitiveservlet implements the standard servlet interface. We simply implemented other methods of the servlet life cycle, and in the service () method we did the simplest operation of spitting text into the browser.
2.4 Servletprocessor and Staticresourceprocessor
650) this.width=650; "id=" code_img_closed_d294e1ab-d06f-489b-96a3-c34add353412 "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:none;vertical-align:middle; "/> View Code
Servletprocessor is the class that handles Serlvet requests. Its purpose is to instantiate the corresponding servlet based on the requested path, and execute the Service () method of the servlet.
650) this.width=650; "id=" code_img_closed_5cab1b8a-601f-4130-bcdf-34ad85fbe04b "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:none;vertical-align:middle; "/> View Code
Staticresourceprocessor simply calls the response Sendstaticresource () method to return the static resource.
Finally, we need to build a helper class constants specifies the storage path for static resources
650) this.width=650; "id=" code_img_closed_cedd3fb0-3a32-412b-aed6-abde27201300 "class=" code_img_closed "src="/img/ Jia.gif "style=" margin:0px;padding:0px 5px 0px 0px;border:none;vertical-align:middle; "/> View Code
3 starting the server
start the main function to start our HTTP server. Http://localhost:8080/test in Browser input, results 3.1
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/912007/201606/912007-20160601153547508-2024550056. PNG "width=" 491 "height=" 173 "style=" margin:0px;padding:0px;border:none; "/>
Figure 3.1
This URL accesses a file in My computer test, whose storage path is/users/wangyu/documents/workspace/tomcat/webroot/test, which is under the Webroot directory of the current project.
When Http://localhost:8080/v2/PrimitiveServlet is entered in the browser
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/912007/201606/912007-20160601153433274-1907713620. PNG "width=" 630 "height=" 174 "style=" margin:0px;padding:0px;border:none; "/>
Figure 3.2
This is consistent with the output of our primitiveservlet.
4 Conclusion
Of course, Tomcat is much more powerful than our simple HTTP server. But does this simplest HTTP server give you a deeper understanding of the HTTP server?
Understanding Tomcat's build simple HTTP server