Implement Web Server with Java [转]

xiaoxiao2021-03-06  69

Computer World 1999 Xu Huiyi, the role of the HTTP protocol, the principle of the HTTP protocol, including four steps: 1. Connection: Web browser establishes a connection with 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 requests is as follows: 1. Create a ServerSocket class object to listen 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 ()); DAT aInputStreaminstream new DataInputStream (client.getInputStream ()); String inline = instream.readLine (); // read the request submitted System.out.println Web browser ( "Received:" inline); if (getrequest (inline)) {// If it is a GET request String filename =

GetFileName (Inline); file file = new file (file file); if (file.exists ()) {// If the file exists, send the file to the web browser system.out.println (FileName Requested. "); Outstream.println ("http / 1.0200ok"); 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 does not exist String Notfound = "htmlheadTitle Not Found / title / head bodyhlError404-File notfound / hl / body / html "; outstream.println (" HTTP /1.0 404 no found "); outstream.println (" Content_Type: text / html "); outstream.println (" Content_Length: NOTFOUND.LENGTH () 2); outstream.println (""); outstream.println (notfound); outstream.flush ();}} long m1 = 1; while (m10) {ixg (s.substring (0, 3). EqualsignoreCase ("get")) Return true;} Return False;} / * Get the file name to be accessed * / string getFileName (String s) {string f = s.substring (S.Indexof (/) 1) ; F = f.substring (0, f.indexof (/)); try {f (f.char At (0) == /) f = f.substring (1);} catch (string indexoutofboundsexception e) {system.out.println ("Exception:" e);} if (f.equals (")) f = "index.html"; returnif;

转载请注明原文地址:https://www.9cbs.com/read-109884.html

New Post(0)