Question
What is the simplest method to create an HTTP server in Java, specifically for handling GET and POST requests without an application 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;
public class SimpleHttpServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null);
server.start();
System.out.println("Server started on port 8080");
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String response;
if (exchange.getRequestMethod().equalsIgnoreCase("GET")) {
response = "This is a response to GET request";
} else if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
response = "This is a response to POST request";
} else {
response = "Method not supported";
}
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Answer
Creating a simple HTTP server in Java can be straightforward, especially with the Built-in `HttpServer` class available in the `com.sun.net.httpserver` package. This implementation allows you to respond to both GET and POST requests without the need for a full-fledged application 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;
public class SimpleHttpServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null);
server.start();
System.out.println("Server started on port 8080");
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String response;
if (exchange.getRequestMethod().equalsIgnoreCase("GET")) {
response = "This is a response to GET request";
} else if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
response = "This is a response to POST request";
} else {
response = "Method not supported";
}
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Causes
- You need a lightweight server solution in Java.
- Your project requirements specify handling of GET and POST requests.
- Avoiding the overhead of a large application server.
Solutions
- Utilize the `HttpServer` class from the `com.sun.net.httpserver` package to create a basic HTTP server.
- Implement a custom handler to manage incoming requests and provide responses accordingly.
- Ensure your server listens on a specified port and can handle multiple request types.
Common Mistakes
Mistake: Not specifying the server port correctly.
Solution: Ensure the port number is open and accessible; in the code above, it's set to 8080.
Mistake: Ignoring request method handling, leading to unhandled requests.
Solution: Always check the request method (GET or POST) before responding.
Mistake: Forgetting to set response headers appropriately.
Solution: Use `exchange.sendResponseHeaders()` to set response codes and sizes.
Helpers
- HTTP server in Java
- Java simple server
- GET POST Java server
- Java HttpServer example
- Creating HTTP server Java