Skip to content Skip to sidebar Skip to footer

Http Url For Js File Inside A War?

Suppose a WAR layout like so: foo.war -->/WEB-INF -->/classes (..) -->/js -->bar.js -->index.jsp -->web.xml Now suppose the WAR's virtual dire

Solution 1:

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.


Solution 2:

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.


Post a Comment for "Http Url For Js File Inside A War?"