Simple HTTP server in Java using only Java SE API
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Creating a simple HTTP server in Java using only the standard Java SE API can provide lightweight web services without needing additional libraries such as Spring or Jakarta EE. This is especially useful for testing, prototyping, or for when you need a simple backend for an application without the overhead of deploying an entire application server.
Java HTTPServer Class Overview
The core class utilized for this purpose in Java is the HttpServer provided by the com.sun.net.httpserver package, which was introduced in Java 6. This package isn't part of the Java standard, but Oracle includes it in its JDK. It's important to note that this server is very basic and is suitable for handling low to moderate load environments.
Creating a Simple HTTP Server
The process of setting up a basic server involves several steps:
- Instantiate the Server: Create an instance of
HttpServer. - Create Contexts: Define context objects which handle the routing of incoming requests.
- Start the Server: Start the server so it begins listening to incoming client requests.
Here is a simple example to demonstrate how this can be done:
In this basic example, the server listens on port 8000 and responds to HTTP requests at the /test route by returning a plain text response.
Request Handling
Each request is handled by an implementation of the HttpHandler interface, which provides a single method handle, equipped with an HttpExchange object. The HttpExchange object encapsulates a request received and a response to be generated. You can do more complex request handling, including parsing request headers and returning various types of responses.
Custom Executing
By default, the server uses a system-built executor – this means it handles requests using a default execution strategy which is suitable for a small number of concurrent connections. However, you can set a custom executor to improve the concurrency and performance of your server like this:
Summary Table
The table below summarizes the key components of setting up a HttpServer:
| Component | Role in Server Setup |
| HttpServer | Server instance which listens for connections |
| InetSocketAddress | Binds server to a specific port |
| HttpHandler | Handles the specifics of each routed request |
| HttpExchange | Manages data about an HTTP exchange (request/response) |
Limitations
Though Java's built-in HTTP server is functional for simple scenarios, it lacks many features necessary for full-fledged web applications, such as advanced routing, complex error handling, and integrated security features. For more sophisticated requirements, consider using more robust solutions.
Conclusion
Using Java SE's API for building an HTTP server can be incredibly handy for small-scale projects and learning purposes. This approach provides an accessible means to understand the basics of web server operations, HTTP protocol, and handling with pure Java. For more robust application needs, considering external libraries or a complete application framework would be beneficial.
Related reading
- Simple way for message passing in distributed system
- Simulating 2 phase distributed commit failures using grpc
- Skipping service no endpoints found when attempting to fetch certificate with traefik 2 cert-manager http-01 challenge
- Sliding window of a batch in Tensorflow using Dataset API
- Simplest and understandable example of volatile keyword in Java
- Simplest way to read JSON from a URL in Java
- SOAP vs REST (differences)
- Socket hang up using axios or request to get data from google geocoding api

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.