Java embedded jetty:资源文件夹中的服务器html文件
我正在尝试在我的应用程序中使用嵌入式jetty服务器。我想提供一个只使用html和javascript的html页面。我正在使用maven,我将文件放在Java embedded jetty:资源文件夹中的服务器html文件,java,embedded-jetty,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();
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文件夹中,有我的索引。HTML您的
ServletContextHandler
设置在的contextPath
处
这意味着您对index.html
的访问应该位于
http://localhost:7498/index.html
不是
打印出运行期间webRootUri
的值,并将该信息添加到您的问题中。感谢您的回复!我加上了,我不明白。JettyServer文件夹从何而来?我应该把我的html文件放在哪个项目文件夹?明白了!在gui类中有一个直接设置为字符串的路径,我没有看到。并且加载了该路径而不是我的路径。谢谢
http://localhost:7498/JettyServer/index.html