Java 404找不到请求的上下文
如下图所示,我的HTML索引页位于静态文件夹中。如何将localhost指向Java 404找不到请求的上下文,java,intellij-idea,Java,Intellij Idea,如下图所示,我的HTML索引页位于静态文件夹中。如何将localhost指向static/index.html 我自己试过基本的hello world static class MyHandler implements HttpHandler { public void handle(HttpExchange t) throws IOException { String response = "Hello world"; t.sendRespon
static/index.html
我自己试过基本的hello world
static class MyHandler implements HttpHandler {
public void
handle(HttpExchange t) throws IOException {
String response = "Hello world";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes()); os.close();
}
}
我需要知道如何将默认的localhost:8080指向预构建的现有static/index.html页面?如果我在应用程序启动时正确理解了您的问题,您希望将页面显示为默认页面吗 请通过下面的链接
这将让您知道如何配置web.xml页面,让tomcat或任何其他web容器知道您的默认页面 你可以试试这个
public class Test {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/welcomePage", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "This is the response";
// we do not set content type, headers, cookies etc.
// resp.setContentType("text/html"); // while redirecting as
// it would most likely result in an IllegalStateException
// "/" is relative to the context root (your web-app name)
RequestDispatcher view = req.getRequestDispatcher("/path/to/file.html");
// don't add your web-app name to the path
view.forward(req, resp);
}
}
}
没有雄猫。我正在运行main()我在github alsoI托管的项目中没有看到任何web.xml,我猜您可能会将其绑定为默认主页的默认上下文“/”。我已更新了答案。。请让我知道它是否适合你。否则我们可能会找到更好的方法。您是说您无法导入RequestDispatcher类吗?