gpt4 book ai didi

java - Grizzly 服务器索引页无法正常工作

转载 作者:行者123 更新时间:2023-11-30 02:34:14 26 4
gpt4 key购买 nike

我有这个服务器设置和路由代码。

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 工作正常。

最佳答案

尝试改变 //webServer = HttpServer.createSimpleServer(); //webServer.getServerConfiguration().setName("Neo CMS");

webServer = new HttpServer();
NetworkListener nl = new NetworkListener("Neo CMS", "localhost", 8080);
webServer.addListener(nl);

关于java - Grizzly 服务器索引页无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43518773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com