我有一个 WAR(通过嵌入式 Jetty 提供服务),我想在 WAR 之外 包含静态文件。我的目标是:
- 从 WAR 之外的路径提供静态文件,相对于嵌入式 Jetty 运行的目录。所以,如果我调用
/srv/app
中的 jar,我想配置一个 URLhttp://myapp.com/static/js/my.js
服务于/src/app/public/js/my.js
。 - 安全:没有目录列表或
../
路径转义 - 不是重新发明轮子:我不想编写新的 Servlet,而是利用现有功能
- 理想情况下(不需要):隐藏 WAR 中的静态内容,这样如果我有一个
/src/app/public/index.html
,就会提供该文件,并且,如果不存在,将提供 WAR 的index.html
。
这是我检查的内容,以及我卡住的地方:
- DefaultServlet:看起来很理想,但是,我找不到任何方法让它为 WAR 之外的文件提供服务。 有没有办法让 DefaultServlet 在 WAR 之外提供静态内容,路径与应用的密码相关?
- 配置 Jetty,如 https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html 中所述;这似乎是理想的,但是,当复制进来时,似乎没有做任何事情(这是一个 NOOP)。
- ResourceHandler:这样做正确吗?我无法弄清楚如何使用它来实现我的目标
最佳答案
ResourceHandler: Is this the right way to do it? I wasn't able to figure out how to use this to make my goals
不要使用ResourceHandler
,它不如DefaultServlet
。
Configuring Jetty, as described in https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html ; this would seem to be ideal, but, when copied in, didn't seem to do anything (it was a NOOP).
这仅在您不与部署的网络应用共享相同的 contextPath
时有效。
Is there a way for the DefaultServlet to serve static content outside the WAR, with paths relative to the app's pwd?
是的,这已经以几种不同的方式记录在 stackoverflow 上。
关键是,您将使用额外的 DefaultServlet
条目,在不同的 url-模式
。
参见:
关于servlets - 使用嵌入式 Jetty 在 WAR 之外提供静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101523/