This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Can't locate .js (or .css) files from a .jsp page. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can Watch "Can New topic
Author

Can't locate .js (or .css) files from a .jsp page.

Al Koch
Greenhorn

Joined: Aug 24, 2011
Posts: 24

Hello,

I am running Tomcat 5.54 app server from Eclipse Indigo under Windows 7 Ultimate. My servlet is using a .jsp and the .jsp page contains (among others) the following to "include" some JavaScript into the .jsp:
<script src="./WebContent/WEB-INF/JSP/Scripts/Modernizr-2.5.3.js" asynch defer></script>

When the servlet runs the .jsp page is displayed but I get 404 errors like "The requested resource (/MyServer/WebContent/WEB-INF/JSP/Scripts/Modernizr-2.5.3.js) is not available." on all of the .js and .css files that are included in the .jsp page.

The relevant file locations are:
./WEB-INF/JSP the .jsp file
./WEB-INF/JSP/Scripts the .js file
./WEB-INF/JSP/CSS the .css files

In particular, Modernizr-2.5.3.js is located (as managed by Eclipse) at .\Projects\MyServer\WebContent\WEB-INF\JSP\Scripts\Modernizr-2.5.3.js. I understand that the path to "included" content such as .js, .css, images and such is relative to the location of the submitted URL, not relative to the displayed .jsp and it is my understanding that the best way to handle this is to use a fully qualified path in the src= URL, which I believe I am doing. However, I get 404 errors. I have tried placing Modernizr-2.5.3.js (as a test case) "all over the place" and I have never been able to eliminate the 404 errors. (I realize I can pull this particular script from the web but that doesn't solve the problem that I have.)

Do I have a mistake in the src= URL? If not, what else would cause these 404 errors?

Thanks for the help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Al Koch wrote:Do I have a mistake in the src= URL?


Yes, you do. It's quite possible that you haven't specified that relative URL correctly in terms of what it's relative to, but even if you had, the server won't serve any resources which are under the application's WEB-INF directory.

So don't put resources there if the browser needs to ask for them. That clearly applies to JS and CSS, and there might be other resources you have which it applies to as well.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

And once you've moved them out of WEB-INF, see the JspFaq to learn how to create proper server-relative URLs that will be bullet-proof.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And one more thing -- the URLs shouldn't include anything related to Eclipse specifically, such as the WebContent directory. They should represent paths relative (see Bear's link) to your web application. Eclipse should have nothing to do with your configuration.
Al Koch
Greenhorn

Joined: Aug 24, 2011
Posts: 24

Thank you Paul & Bear. Once you pointed out that WEB-INF is not accessible from the web I started understanding. I created a folder at the same level as WEB-INF to contain the resources and it's now working. In doing more research I came across http://help.eclipse.org/indigo/topic/org.eclipse.wst.webtools.doc.user/topics/ccwebprj.html which I found really useful.

I'd sure like to know why a folder that can't be accessed "from the WEB" was named "WEB-INF". I interpreted that to mean "WEB-INFORMATION" and that actually suggested to me that this was exactly where I was supposed to the resources!

Thank you for the help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

I would suggest read the Servlet Specification. It's a surprisingly easy read.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't locate .js (or .css) files from a .jsp page.