Created
May 7, 2014 07:20
-
-
Save sjyun/cab188a22a755914ce90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test0911; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.InetSocketAddress; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
import com.sun.net.httpserver.HttpServer; | |
public class SimpleServer { | |
private static final int PORT = 7080; | |
public static void main(String ar[]) throws IOException { | |
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0); | |
server.createContext("/home", new WebserverHandler()); | |
server.start(); | |
System.out.println("started"); | |
} | |
static class WebserverHandler implements HttpHandler { | |
@Override | |
public void handle(HttpExchange t) throws IOException { | |
System.out.println("accepted"); | |
String response = "web server test"; | |
t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length()); | |
OutputStream os = t.getResponseBody(); | |
os.write(response.getBytes()); | |
System.out.println("complete"); | |
os.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment