我有这个服务器设置和路由代码。
package net.randohinn.neo;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
public class Main {
public static HttpServer webServer;
public static void main(String[] args) {
webServer = HttpServer.createSimpleServer();
webServer.getServerConfiguration().setName("Neo CMS");
webServer.getServerConfiguration().addHttpHandler(
new HttpHandler() {
public void service(Request request, Response response) throws Exception {
final SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
final String date = format.format(new Date(System.currentTimeMillis()));
response.setContentType("text/plain");
response.setContentLength(date.length());
response.getWriter().write(date);
}
},
"/");
try {
webServer.start();
Thread.currentThread().join();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果我现在转到localhost:8080,它告诉我路径/的资源不存在:(好像它无法检测到首页的处理程序..这里有什么问题?改变路径例如,在/time
的处理程序中,工作正常。
答案 0 :(得分:2)
尝试改变 // webServer = HttpServer.createSimpleServer(); //webServer.getServerConfiguration().setName("Neo CMS“);
webServer = new HttpServer();
NetworkListener nl = new NetworkListener("Neo CMS", "localhost", 8080);
webServer.addListener(nl);