从 jar 中交付 javascript
我有一个java网络应用程序。这里我有一些 javascript 文件,我想将它们放在 jar 文件中。我希望有一个 servlet 可以从 jar 中传递 javascript 文件。该功能类似于 DWR 库。他们有一些未包含在文件系统中的 javascript 文件。相反,他们从 jar 中传递 javascript。 DWRServlet 类可以做到这一点。但该路径包含在 html 标头中。我想实现这样一个功能。你们能给我一些如何实现的想法吗
I have a java web application. Here I have some javascript files which I want to be inside a jar file. I want there will be a servlet which will deliver the javascript files from the jar. The feature is like DWR library. They have some javascript files which is not included in the file system. Rather they deliver the javascripts from the jar. DWRServlet class do that. But the path is included in the html header. I want to implement such a feature. Could you guys give me some idea how to implement that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JAR 中的资源是类路径的一部分。您可以通过
ClassLoader#getResourceAsStream()
获取类路径资源的InputStream
。因此,让您的 Servlet 完全执行此操作即可。假设您在 JAR 的
/META-INF/resources
中有这些 JS 资源:那么您可以通过以下方式获取 JAR 的
/META-INF/resources/some.js
http://localhost:8080/contextname/resources/some.js。Resources in JARs are part of the classpath. You can get an
InputStream
of a classpath resource byClassLoader#getResourceAsStream()
. So, just let your Servlet do exactly that.Assuming that you have those JS resources in
/META-INF/resources
of the JAR:Then you can get
/META-INF/resources/some.js
of the JAR by http://localhost:8080/contextname/resources/some.js.