public class MyApp {
public static void main(String[] args) {
WebServer webServer = WebServers.createWebServer(9996)
.add(new HelloWorldHandler());
webServer.start();
System.out.println("Listening on " + webServer.getUri());
}
}
I'm confusing.
On Monday, September 24, 2012, Bruce Chen wrote:
I copy the code from https://github.com/webbit/webbit/wiki/Writing-HttpHandlerspublic class MyApp { public static void main(String[] args) { WebServer webServer = WebServers.createWebServer(9996) .add(new HelloWorldHandler()); webServer.start(); System.out.println("Listening on " + webServer.getUri()); } }
If I run it, webServer will exist at the end of main,while I debug it, webServer will run.It's running in a daemon thread.
I didn't stop the webServer, but it existed, I think the webServer thread already stopped.
I think webServer.start() in main is start the webserver thread and webServer still run when the main exist;if I want to stop the webserver, I must call webserver.stop().But in the demo code, the webserver stopped while main existed.
Sorry, problem is still on,here is my demo code.[code]public class HelloWorldHandler implements HttpHandler {@Overridepublic void handleHttpRequest(HttpRequest request, HttpResponse response,HttpControl control) throws Exception {// TODO Auto-generated method stubresponse.header("Content-type", "text/html").content("<html><body>Hello world!</body></html>").end();}}public class WebServerDemo {public static void main(String[] args) throws InterruptedException, ExecutionException, MalformedURLException, IOException{startServer();}public static void startServer() throws InterruptedException, ExecutionException, MalformedURLException, IOException{Future<? extends WebServer> f = WebServers.createWebServer(9996).add("/",new HelloWorldHandler()).start();WebServer webServer = f.get();System.out.println(webServer.getUri().toString());}}[/code]I run it, then visit "http://localhost:9996",but the webserver isn't running.Now, my question is simple: why I can't visit the "http://localhost:9996" ?
在 2012年9月24日星期一UTC+8下午11时42分57秒,Aslak Hellesøy写道:
Oh, I'm so stupid, I used webbit-0.4.9-full.jar as lib, so webserver can' work when main exist.