嵌入式Jetty在其Jar文件中查找文件

LaS*_*bra 21 jsp embeddedwebserver jar jetty embedded-jetty

我成功地将Jetty嵌入到测试应用程序中.它可以毫无问题地提供文件.现在我想知道Jetty是否可以提供其自己的Jar文件中的文件.

有谁知道这是否可能?

Uri*_*ter 38

Jetty嵌入页面上列出了一个示例:http://docs.codehaus.org/display/JETTY/Embedding+Jetty

诀窍是为类路径位置创建文件URL.

String webDir = this.class.getClassLoader().getResource("com/company/project/mywebdir").toExternalForm();

ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.setResourceBase(webDir);
Run Code Online (Sandbox Code Playgroud)


Jam*_*mar 5

如果你把Spring扔进这个等式,这很简单.它在这里:

 ...

 WebAppContext webAppContext = new WebAppContext();
 webAppContext.setServer(server);
 webAppContext.setContextPath("/");
 webAppContext.setResourceBase(new ClassPathResource("webapp").getURI().toString());

 server.addHandler(webAppContext); 

 ....

这将使jetty在jar文件中找到必要的Web资源.


lod*_*d3n -4

也许更多的是 hack,但 JAR 文件实际上不是 ZIP 吗?(不确定)您能否将它们解压缩到临时文件夹中并从那里提供它们?