Implement web server with Java
Xu Hui, Guangxi Financial College
I. The role of the HTTP protocol The principle of the HTTP protocol includes four steps: 1. Connection: The web browser establishes a connection to the web server. 2. Request: The web browser submits a request to the web server via Socket. 3. Answer: After the web browser submits the request, transfer to the web server via HTTP. After the web server receives the request, transaction processing, the processing result is sent back to the web browser via HTTP, so that the requested page is displayed on the web browser. 4. Relationship connection: After the response is completed, the web browser must be disconnected to ensure that other web browsers can connect with the web server. Second, using Java to implement the web server programming According to the principle of the above HTTP protocol, the method of implementing the web server program for GET request is as follows: 1. Create a ServerSocket class object, listening to port 8080. This is to distinguish between the standard TCP / IP port 80 of HTTP; 2. Waiting, accept the client to connect to the port 8080 to obtain the socket; 3. Create input stream INSTREAM associated with socket, Stream OutStream; format: GET path / file name HTTP / 1.0; 4. Read the request information submitted by the client from the input stream INSTREAM associated with the socket, requesting the format of the information: GET path / file name HTTP / 1.0 ; 5. Get the request type from the request information. If the request type is GET, get the HTML file name accessed from the request information. When there is no html file name, then index.htm1 is used as a file name; 6. If the HTML file exists, open the HTML file, transfer the http header information and the HTML file content to the web server via Socket, then close the file, otherwise send an error Information gives a web browser; 7. Close the Socket word connected to the corresponding web browser. The following program is written in accordance with the above method, which enables multi-threaded web servers to ensure that multiple clients can connect to the web server.
//Webserver.java writes web server import java.io. *; import java.Net. *; Import java.util.date; public class webserver {public static void main (string args [= 1) {int i = 1 , Port = 8080; Serversocket Server = null; socketclient = null; try {server = new serversocket (port); system.out.println ("Web Server IS Listening on Port" Server.getlocalPort ()); for (;;) {Client = server.accept (); // Accept the connection of the client New Connection Thread (Client, i) .Start (); i ;}} catch (exception e) {system.out.println (e);} }}} / * Connnection Thread class completes communication with a web browser * / Class Connection Thread Extends Thread {socket client; // Connect the web browser's socket word int counter; // counter PUBLIC Connection Thread (socketcl, INT C) {Client = CL; counter = C;} PUBLIC VOID RUN () // Wire body {Try {string deskip = client.getinetaddress (). TOSTRING (); // client IP address INT DESTPORT = client.getPort (); // client port number System.out.println ( "Connecction" counter ": connected to" destIP "on port" destport "."); PrintStream outstream = new printStream (client.getOoutputStream ()); DataInputStreaminstream new DataInputStream (clien T.GETINPUTSTREAM ()); string inline = instream.readline (); // reads the request information submitted by the web browser system.out.println ("received:" inline); if (getRequest (inline) {////// If it is a GET request string filename = getFileName (inline); file file =
New file (filename); if (file.exists ()) {// If the file exists, give the file to the web browser system.out.println (FileName "Requested."); Outstream.Println ("http / 1.0200 OK "); OutStream.println (" MIME_VERSION: 1.0 "); OutStream.println (" Content_Type: Text / HTM1); int Len = (int) file.length (); Outstream.println ("Content_length:" LEN) OutStream.println (""); sendfile (outstream, file); // Send file outstream.flush ();} else {// file String notfound = "