⬆️ ⬇️

HTTP server in 15 minutes

Task



For the minimum time to write an HTTP server, which, after launching, will be able to correctly respond to the browser and give a simple HTML page (the minimum time for the code to be short, for the beginner to understand more easily).

It took me about 15 minutes. The server seems to cope with the task.



The essence of the example is to show what Socket, ServerSocket, InputStream, OutputStream, and Thread are.





Decision



 import java.net.ServerSocket;
 import java.net.Socket;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.InputStreamReader;
 import java.io.BufferedReader;

 / **
  * Created by yar 09/09/2009
  * /
 public class HttpServer {

     public static void main (String [] args) throws Throwable {
         ServerSocket ss = new ServerSocket (8080);
         while (true) {
             Socket s = ss.accept ();
             System.err.println ("Client accepted");
             new Thread (new SocketProcessor (s)). start ();
         }
     }

     private static class SocketProcessor implements Runnable {

         private socket
         private InputStream is;
         private OutputStream os;

         private SocketProcessor (Socket s) throws Throwable {
             this.s = s;
             this.is = s.getInputStream ();
             this.os = s.getOutputStream ();
         }

         public void run () {
             try {
                 readInputHeaders ();
                 writeResponse ("<html> <body> <h1> Hello from Habrahabr </ h1> </ body> </ html>");
             } catch (Throwable t) {
                 / * do nothing * /
             } finally {
                 try {
                     s.close ();
                 } catch (Throwable t) {
                     / * do nothing * /
                 }
             }
             System.err.println ("Client processing finished");
         }

         private void writeResponse (String s) throws Throwable {
             String response = "HTTP / 1.1 200 OK \ r \ n" +
                     "Server: YarServer / 2009-09-09 \ r \ n" +
                     "Content-Type: text / html \ r \ n" +
                     "Content-Length:" + s.length () + "\ r \ n" +
                     "Connection: close \ r \ n \ r \ n";
             String result = response + s;
             os.write (result.getBytes ());
             os.flush ();
         }

         private void readInputHeaders () throws Throwable {
             BufferedReader br = new BufferedReader (new InputStreamReader (is));
             while (true) {
                 String s = br.readLine ();
                 if (s == null || s.trim (). length () == 0) {
                     break;
                 }
             }
         }
     }
 }


')

How to start



1) Create HttpServer.java file

2) Insert the source text into this file.

3) Compile with javac HttpServer.java

4) Run the java -cp command. HttpServer (with port 8080 must be free)

5) Open the browser and go to http: // localhost: 8080 /

Source: https://habr.com/ru/post/69136/



All Articles