In the last article, we introduced the basic knowledge of the HTTP protocol, as well as two important class socket and socketServer classes in Java. Here we will mainly show how to implement a Java-based HTTP server.
The HTTP server is mainly composed of three classes: httpserver, request and response. The inlet of the program is in the HTTPServer class, which calls the AWAIT () method so that Server will wait for the client's connection. When the client is connected, it will send the static page content to the client browser. The following introduces these three classes: 1: httpserver class httpserver needs to have a server root directory This is defined in the web_root variable: public static final string web_root = system.getProperty ("user.dir") file.seParetor " Webroot "; specify the value of the environment variable user.dir when we run the server. The most important way in this class is the AWAIT () method, the content is as follows: public void await () {serversocket serversocket = null; int port = 8080; try {serversocket = new Serversocket (port, 1, inetaddress.getbyName ("127.0. 0.1 "));} catch (ioException e) {E.PrintStackTrace (); system.exit (1);
// Loop waiting for a request while (shutdown!) {Socket socket = null; InputStream input = null; OutputStream output = null; try {socket = serverSocket.accept (); input = socket.getInputStream (); output = socket. Getputstream ();
// Create Request Object and Parse Request Request = New Request.Parse ();
// Create Response Object Response response = new response (output); response.set (request); response.sendStaticResource ();
// close the socket socket.close ();
// check if the previous URI is a shutdown command shutdown = request.getUri () equals (SHUTDOWN_COMMAND);.} Catch (Exception e) {e.printStackTrace (); continue;}}} await () method within a configured ServerSocket The instance, when the client is connected, passes socket.getinputStream () to the Request class, passes socket.getoutputstream () to the Response class, and then pass the Request object to Response, and finally call response.sendStaticResource () The method sends data to the client. SOCKET.CLOSE () After monitoring is accepted commands that shut down Server, if yes, the loop end program is jumped. 2. The main purpose of the Request class REQUEST class is to encapsulate HTTP requests. It has an InputStream type parameter constructor PUBLIC Request (InputStream INPUT) {this.input = INPUT;} At the same time it also has an important String type member The purpose of the variable URI, and Request is to extract URI from InputStream, which is a public void parse () {// read a set of character () {// read a set of characters from the socket stringbuffer request = new stringbuffer (2048); int i; Byte [] buffer = new byte [2048]; try {i = infut.read (buffer);} catch (ioException e) {E.PrintStackTrace (); i = -1;} for (int J = 0; J
private String parseUri (String requestString) {int index1, index2; index1 = requestString.indexOf ( ''); if (= -1 index1!) {index2 = requestString.indexOf ( '', index1 1); if (index2> Index1) Return RequestString.SubString (Index1 1, Index2);} Return Null;} where the Parseuri (String Request) method is a private method, which parses the String parameter returns the string between the two spaces, which is followed If you define the rules of the HTTP request, if you don't know, you can refer to one of the Java-based HTTP servers.