-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleServer.java
More file actions
30 lines (27 loc) · 797 Bytes
/
SimpleServer.java
File metadata and controls
30 lines (27 loc) · 797 Bytes
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
import com.sun.net.httpserver.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.HttpURLConnection;
class SimpleServer
{
public static void main(String[] args) throws IOException
{
int backlog = 0;
InetSocketAddress address = new InetSocketAddress(8000);
HttpServer server = HttpServer.create(address, backlog);
HttpHandler handler = new HttpHandler() {
public void handle(HttpExchange exchg) throws IOException
{
String response = "Response from the server";
exchg.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.getBytes().length);
exchg.getResponseBody().write(response.getBytes());
exchg.close();
}
};
server.createContext("/HomePage", handler);
server.start();
}
}
/*
* http://localhost:8000/HomePage
*/