http url for js file inside a war?

╄→гoц情女王★ 提交于 2019-12-11 01:37:18

问题


Suppose a WAR layout like so:

foo.war
-->/WEB-INF
   -->/classes (..)
   -->/js
      -->bar.js
   -->index.jsp
   -->web.xml

Now suppose the WAR's virtual directory is /blah on server example.com (i.e. http://example.com/blah).

What is the HTTP URL of bar.js, one that would be used in a <script src=""> tag that index.jsp might serve up? http://example.com/blah/js/bar.js doesn't seem to be working.


回答1:


There is no URL that will point to this. Everything within WEB-INF is not exposed to the outside world.

Rather, if you organised the WAR layout like this:

foo.war
-->/WEB-INF
   -->/classes (..)
   -->web.xml
-->/js
  -->bar.js
-->index.jsp

Then you could access your Javascript as http://example.com/blah/js/bar.js.

P.S. You won't be able to access index.jsp either, the way you have things set up at the moment.




回答2:


You must NEVER put a JS inside WEB-INF directory.

As written in Servlet specifications, whatever you put inside WEB-INF directory will never be directly available to the external world. Only local application resources go there.

So if you want some JS file accesible from outside, put it directly at WAR's ROOT. Something like this:

foo.war
-->/js/
    -->bar.js
-->/WEB-INF
    -->internal resources here

The URL to access JS will be something like:

http://YOUR_IP:8080/foo/js/bar.js

This of course could vary depending on how you setup your war deployment on your application server.

You do however put JSP files inside WEB-INF directory, only to invoke them from Servlets (you can't directly access them either) with something like:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("WEB-INF/index.jsp");

This is a common practice if you don't want people accessing directly your JSP files from outside.



来源:https://stackoverflow.com/questions/3891907/http-url-for-js-file-inside-a-war

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