forked from NanoHttpd/nanohttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloServer.java
More file actions
45 lines (40 loc) · 1.1 KB
/
HelloServer.java
File metadata and controls
45 lines (40 loc) · 1.1 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
36
37
38
39
40
41
42
43
44
45
import java.io.*;
import java.util.*;
/**
* An example of subclassing NanoHTTPD to make a custom HTTP server.
*/
public class HelloServer extends NanoHTTPD
{
public HelloServer() throws IOException
{
super(8080, new File("."));
}
public Response serve( String uri, String method, Properties header, Properties parms, Properties files )
{
System.out.println( method + " '" + uri + "' " );
String msg = "<html><body><h1>Hello server</h1>\n";
if ( parms.getProperty("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.getProperty("username") + "!</p>";
msg += "</body></html>\n";
return new NanoHTTPD.Response( HTTP_OK, MIME_HTML, msg );
}
public static void main( String[] args )
{
try
{
new HelloServer();
}
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 t ) {};
}
}