A web server : Web Server « Network Protocol « Java






A web server

   
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MainClass {
  public static void main(String args[]) throws Exception {
    ServerSocket ss = new ServerSocket(80);
    while (true) {
      Socket s = ss.accept();
      PrintStream out = new PrintStream(s.getOutputStream());
      BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
      String info = null;
      while ((info = in.readLine()) != null) {
        System.out.println("now got " + info);
        if (info.equals(""))
          break;
      }
      out.println("HTTP/1.0 200 OK");
      out.println("MIME_version:1.0");
      out.println("Content_Type:text/html");
      String c = "<html> <head></head><body> <h1> hi</h1></Body></html>";
      out.println("Content_Length:" + c.length());
      out.println("");
      out.println(c);
      out.close();
      s.close();
      in.close();
    }
  }
}

   
    
  








Related examples in the same category

1.A simple, tiny, nicely embeddable HTTP 1.0 server in Java
2.Minimal HTTP Server by using com.sun.net.httpserver.HttpServer
3.A simple HTTP server that displays information about all accessible printers on the network
4.POSTing data to an HTTP server