我正在尝试在我的应用程序中使用嵌入式 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/