Horje
How to Set Up a Basic HTTP Server in Java?

In Java, setting up a basic HTTP server involves creating an application that listens for incoming HTTP requests and responses. In this article, we will discuss how to set up a basic HTTP server in Java.

Implementation Steps to Set Up a Basic HTTP Server

  • Step 1: Create an HttpServer instance.
  • Step 2: Create a context and set the handler.
  • Step 3: Start the server.
  • Step 4: Handle the request.

Program to Set up a Basic HTTP Server in Java

Below is the Program to Set up a Basic HTTP Server in Java:

Java

// Java Program to Set up a Basic HTTP Server
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
 
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
 
// Driver Class
public class SimpleHttpServer
{
    // Main Method
    public static void main(String[] args) throws IOException
    {
        // Create an HttpServer instance
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
 
        // Create a context for a specific path and set the handler
        server.createContext("", new MyHandler());
 
        // Start the server
        server.setExecutor(null); // Use the default executor
        server.start();
 
        System.out.println("Server is running on port 8000");
    }
 
    // define a custom HttpHandler
    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException
        {
            // handle the request
            String response = "Hello, this is a simple HTTP server response!";
            exchange.sendResponseHeaders(200, response.length());
            OutputStream os = exchange.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}

Output:

Below the terminal output is showing that the server is running on port number 8000.

Output

Response:

Below we can see the response in browser.

Response in Browser

Explanation of the Code:

  • The above program is the example of the set up a basic HTTP server that can be runs into the port number 8000.
  • It can be develop using HttpServer then the create the instance of the HttpServer after that assign the port number using InetSocketAddress.
  • Then create the new handler once completed the configuration add the response text then start the server.



Reffered: https://www.geeksforgeeks.org


Java

Related
How to Handle a java.lang.ArithmeticException in Java? How to Handle a java.lang.ArithmeticException in Java?
How to Use a PriorityQueue to Implement a Priority-Based Task Scheduler in Java? How to Use a PriorityQueue to Implement a Priority-Based Task Scheduler in Java?
How to Use the ExecutorService.invokeAny Method in Java? How to Use the ExecutorService.invokeAny Method in Java?
How to Make a Java Thread Sleep for a Specific Duration? How to Make a Java Thread Sleep for a Specific Duration?
How to Create a Synchronized HashTable in Java? How to Create a Synchronized HashTable in Java?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13