blob: bd4bc320dce8c391b9e01330bf878ed7b7890b8f (
plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package fi.iki.elonen;
import java.io.*;
import java.util.*;
/**
* An example of subclassing NanoHTTPD to make a custom HTTP server.
*/
public class HelloServer extends NanoHTTPD {
private HelloServer() {
super(8080, new File("."));
}
@Override
public Response serve(String uri, String method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) {
System.out.println(method + " '" + uri + "' ");
String msg = "<html><body><h1>Hello server</h1>\n";
if (parms.get("username") == null)
msg +=
"<form action='?' method='get'>\n" +
" <p>Your name: <input type='text' name='username'></p>\n" +
"</form>\n";
else
msg += "<p>Hello, " + parms.get("username") + "!</p>";
msg += "</body></html>\n";
return new NanoHTTPD.Response(msg);
}
public static void main(String[] args) {
HelloServer helloServer = new HelloServer();
try {
helloServer.start();
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
System.exit(-1);
}
System.out.println("Listening on port 8080. Hit Enter to stop.\n");
try {
System.in.read();
} catch (Throwable ignored) {
}
helloServer.stop();
}
}
|