How to Set Up a Basic HTTP Server in Java? Last Updated : 22 Jun, 2025 Comments Improve Suggest changes Like Article Like Report 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 JavaBelow 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) { try { // 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"); } catch (IOException e) { System.out.println("Error starting the server: " + e.getMessage()); } } // 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.Response:Below we can see the 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.This server is created using HttpServer. First, an instance of HttpServer is created and assigned to a port number using InetSocketAddress.Then create the new handler once completed the configuration add the response text then start the server.The response message is handled in the MyHandler class, where the text "Hello, this is a simple HTTP server response!" is sent to the client. Comment More infoAdvertise with us Next Article How to Set Up a Basic HTTP Server in Java? M maheshkadambala Follow Improve Article Tags : Java Java Programs Java-Networking Java Examples Practice Tags : Java Similar Reads How to implement a Simple DNS Resolver in Java? DNS stands for Domain Name System. For the implementation of the simple DNS resolver in Java is allowed to translate the domain names into the corresponding IP addresses in the programmatically. Domain Name System (DNS) plays a crucial component of internet communication and translates human-readabl 3 min read How to Create a simple TCP Client-Server Connection in Java? TCP can be defined as Transmission Control Protocol. This is the standard protocol for transmitting data over a network. It provides reliable, structured, and error-checked data delivery between applications running on hosts connected via an IP (Internet Protocol) network. In Java, we can create TCP 3 min read How to Secure Communication Using SSL/TLS in Java? Secure Sockets Layer (SSL) or Transport Layer Security (TLS) are cryptographic protocols designed to provide secure communication over the computer network. These protocols are establish an encrypted connection between the client and the server, make sure that the data exchanged between them remains 5 min read How to Implement a Simple Chat Application Using Sockets in Java? In this article, we will create a simple chat application using Java socket programming. Before we are going to discuss our topic, we must know Socket in Java. Java Socket connects two different JREs (Java Runtime Environment). Java sockets can be connection-oriented or connection-less. In Java, we 4 min read How to Implement Peer-to-Peer Communication in Java? Peer-to-peer communication is a decentralized form of communication where two or more devices communicate directly with each other without the need for a central server. In peer-to-peer communication, each peer can act as both a client and a server, enabling them to both send and receive data. Peer- 2 min read Like