开发者

http url for js file inside a war?

开发者 https://www.devze.com 2023-01-19 10:58 出处:网络
Suppose a WAR layout like so: foo.war -->/WEB-INF -->/classes (..) -->/js -->bar.js -->index.jsp -->web.xml Now suppose the开发者_JAVA技巧 WAR\'s virtual directory is /blah on server example.com (

Suppose a WAR layout like so:

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

Now suppose the开发者_JAVA技巧 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消