Embedded Jetty looking for files inside its Jar file

烈酒焚心 提交于 2019-11-27 12:56:21

问题


I successfully embedded Jetty on a test application. It can serve files without issues. Now I want to know if it's possible for Jetty to serve files that are inside its own Jar file.

Does anyone know if that's possible?


回答1:


An example is listed on the Jetty embedding page at http://docs.codehaus.org/display/JETTY/Embedding+Jetty

The trick is to create a File URL to your classpath location.

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

ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.setResourceBase(webDir);



回答2:


It's pretty simple, if you throw Spring into the equation. And here it goes:

 ...

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

 server.addHandler(webAppContext); 

 ....

That will make jetty find the necessary web resources inside the jar file.




回答3:


Maybe more of a hack, but aren't JAR files actually ZIPs? (not sure) Could you unzip them into a temporary folder and serve them from there?




回答4:


Found the answer and it's not Jetty, it's Winstone. http://winstone.sf.net



来源:https://stackoverflow.com/questions/1462953/embedded-jetty-looking-for-files-inside-its-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!