Simple HTTP server in Java using only Java SE API
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

