views:

25

answers:

2

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  A: 

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.

Pablo Santa Cruz
Thanks. I must've misinterpreted a link about packaging using maven-war-plugin.
Steven
+1  A: 

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.

Andrzej Doyle
Thanks. I must've misinterpreted a link about packaging using maven-war-plugin.
Steven