1 Overview
At the final moment of Tomcat startup, A serversocket is enabled to receive all Web requests (most of which are browser requests). After processing the requests, a response is output to the client, such as a browser.
Tomcat supports the HTTP application layer protocol, and the browser requests resources from Tomcat using the HTTP protocol.
The focus of this article is to simulate the Tomcat service through simple classes. Note that the development of sample code must follow the HTTP protocol.
2 code Display 2.1 testsocket service class
The testsocket class is used to simulate the Tomcat service and run its main method to start the service. The key methods and attributes of testsocket are described as follows:
| Name |
Type |
Description |
| Serversocket |
Attribute |
It is used to listen to all socket connections. It is used to listen to socket connections from the browser. |
| Poolexecuter |
Attribute |
Provide the receiver. the receiver is used to receive the response message after the socket is received.This design can makeTomcatReceiving socket with no server BlockingRequest. |
| Requestcount |
Attribute |
Used to count the number of requests. |
| Initpoolexecuter |
Method |
Initialize Thread Pool |
| Startserver |
Method |
Start the service |
|
|
|
The complete code is as follows:
Package socket; import Java. io. ioexception; import java.net. serversocket; import java.net. socket; import Java. util. concurrent. linkedblockingqueue; import Java. util. concurrent. threadpoolexecutor; import Java. util. concurrent. timeunit; import thread. acceptor; public class testsocket {public testsocket () {initpoolexecuter (); startserver ();} private serversocket; private threadpoolexecutor poolexecuter; private int requestcount = 0; Public void reply () {worker blockingqueue <runnable> workqueue = new worker blockingqueue <runnable> (); poolexecuter = new threadpoolexecutor (2, 2, 60, timeunit. minutes, workqueue);} public void startserver () {try {system. out. println ("Tomcat service started"); serversocket = new serversocket (8080, 2); While (true) {Socket socket = serversocket. accept (); handlersocket (socket) ;}} catch (ioexception e) {e. printstacktrace () ;}}/*** process request * @ Param socket * @ throws ioexception */Public void handlersocket (Socket socket) throws ioexception {requestcount ++; system. err. println ("Number" + requestcount + "requests from the client"); poolexecuter. submit (New Acceptor (socket);} public static void main (string [] ARGs) {New testsocket ();}}2.2 acceptor receiving Class
The acceptor is the runnable implementation class, that is, the thread, which is provided in testsocket through poolexecuter. This design idea can implement asynchronous Tomcat receiving and processing requests.
The acceptor class reads data from the inputstream of the socket. After data processing is completed, the outputstream of the socket outputs the response. In this example, all the data information in inputstream is simply output, and a response packet is defined and returned to the browser.
Two key points need to be elaborated. Tomcat's socket request comes from the browser, and inputstream in Tomcat receives the request message when the browser initiates the request. The message follows the HTTP protocol specification; after Tomcat finishes processing the request and outputs the response data, Tomcat should also generate the response packet according to the HTTP protocol and output it to the browser so that the browser can correctly parse the processing result returned by Tomcat.
The complete code is as follows:
Package thread; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import java.net. socket; public class acceptor implements runnable {private Socket socket; Public acceptor (Socket socket) {This. socket = socket ;}@ overridepublic void run () {outputstream outstream = NULL; try {inputstream instream = socket. getinputstream (); byte [] READ = new byte [instream. available ()]; instream. Read (read); system. err. println ("print request message:"); system. out. write (read); string response = "HTTP/1.1 200 OK \ n" + "Content-Type: text/plain \ n" + "Content-Length: 12 \ n "+" \ n "+" Hello world! "; System. err. println ("printed return message:"); system. out. println (response); outstream = socket. getoutputstream (); outstream. write (response. getbytes ();} catch (exception e) {e. printstacktrace ();} finally {try {outstream. flush (); outstream. close ();} catch (ioexception e) {e. printstacktrace ();}}}}3. Questions to be figured out
Understanding the HTTP protocol and the process of simulating Tomcat services and requests after a browser initiates a socket request to Tomcat following the HTTP protocol is simple. If Tomcat does not generate a response packet according to the HTTP protocol after processing the request, different browsers will render Tomcat output differently. In general, there will be problems.
When a browser accesses the Tomcat service, the number of serversocket requests from different browsers is different. The reason is that the implementation of different types of browsers is not the same.The specific phenomenon is to use360Fast browser access to TomcatService, a request process, can be monitored to a maximum of 3Secondary socketConnection, sometimes twice, but IE8 is usedAccess TomcatService, one request process, only one socketConnection.Although the number of times the browser initiates a socket is different when a request is initiated, the final effect is the same as that of the response packet output following the HTTP protocol, only the entity content of the Response Message is displayed in the browser.
The second problem is about the thread code execution sequence. My code is as follows:
| System. Err. println ("print request message :"); System. Out. Write (read ); System. Err. println ("printed return message :"); System. Out. println (response ); |
The output result of the above Code is often:
In this way, the Code does not correspond to the output. I guess this is thread..
Finally, you are welcome to discuss these problems.