Giter VIP home page Giter VIP logo

java-web-server's Introduction

Java web server example

Example code to make your own Java Web Server which prints Hello World

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {
    public static void main(String[] args) throws IOException {
        System.out.println("Server is running at http://127.0.0.1:3000");

        ServerSocket serverSocket = new ServerSocket(3000);
        while (true) {
            // Wait for the new connection and block the further execution.
            Socket socket = serverSocket.accept();

            // Great, browser is connected to our web server
            // Print writer is used to write buffered string text. It will not write to the socket until it's buffer is filled.
            // Writing to socket at once is faster than writing in very small chunks.
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
            // Http Header starts
            // Http version and status code for the browser to handle response
            printWriter.write("HTTP/1.1 200 OK\r\n");
            // We are saying browser, this is a html file
            printWriter.write("Content-Type: text/html\r\n");
            // Http header ends with \r\n
            printWriter.write("\r\n");

            // We now write actual response
            printWriter.write("<h1>Hello World</h1>");
            // If buffered is filled, probably response is already sent. If it's waiting for the buffer to filled,
            // we need to flush it to make sure all the data is sent to the browser.
            printWriter.flush();

            // Closing socket is the simplest way to say to the browser that all data has been sent.
            socket.close();
        }
    }
}

java-web-server's People

Contributors

tejmagar avatar

Stargazers

Rajesh avatar Kritish Neupane avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.