java - 嵌入式 jetty : server html file from resource folder

标签 java embedded-jetty

我正在尝试在我的应用程序中使用嵌入式 jetty 服务器。我想提供一个仅使用 html 和 javascript 的 html 页面。我正在使用 Maven,并将文件放在 src/main/resources/html/ 中。

这是我的代码:

 Server server = new Server(7498);

    URL url = Main.class.getClassLoader().getResource("html/");

    URI webRootUri = url .toURI();

    ServletContextHandler context = new ServletContextHandler(
            ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setBaseResource(Resource.newResource(webRootUri));
    context.setWelcomeFiles(new String[] { "index.html" });

    ServletHolder holderPwd = new ServletHolder("default",
            DefaultServlet.class);
    holderPwd.setInitParameter("dirAllowed", "true");
    context.addServlet(holderPwd, "/");

    server.setHandler(context);

    try {
        server.start();
        server.dump(System.err);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

我收到此错误:

Problem accessing /JettyServer/index.html. Reason:

    Not Found

为什么它仍然出现在/JettyServer/index.html 中?如何让它在资源文件夹中查找?

编辑

webRootUri 是

file:/D:/Workspaces/Eclipse/Eclipse_SE/CDP/target/classes/html/

在该 HTML 文件夹中,有我的 index.html

最佳答案

您的 ServletContextHandler 设置为 "/"contextPath

这意味着您对 index.html 的访问应该位于

http://localhost:7498/index.html

不是

http://localhost:7498/JettyServer/index.html

关于java - 嵌入式 jetty : server html file from resource folder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34155407/

相关文章:

Java程序在cmd中执行而不使用设置路径或系统变量

java - RxJava - 检查条件并仅在条件为真时重复一次

java - 如果没有抛出异常则返回 boolean true 表示操作成功?

jax-rs - RESTEasy 支持 JAX-RS @RolesAllowed

jetty - 如何将 CXFNonSpringJaxrsServlet 与 serviceBean 而不是 serviceClass 一起使用?

java - BufferingResponseListener 和 getContentAsString 追加之前获取的内容

java - 重置 eclipse rcp 中保存的状态

java - 如何使用 lambda 表达式计算整数列表中的差异

java - 负载测试期间嵌入式 Jetty 中的内存泄漏

java - 使用jetty启动应用程序时出错