-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHttpServer.java
More file actions
35 lines (29 loc) · 1.18 KB
/
SimpleHttpServer.java
File metadata and controls
35 lines (29 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 {
// Create a new HTTP server on port 8080
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
// Create a context for the "/hello" path and set the handler
server.createContext("/hello", new MyHandler());
// Start the server
server.setExecutor(null); // creates a default executor
server.start();
System.out.println("Server is running on port 8080");
}
// Custom handler for the "/hello" path
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "Hello, this is the response from the server!";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}