This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Access resources in WEB-INF

Nov 17th, 2004, 06:38 PM

Hi, Good day to u.

I am new to spring. I try its web module. I put all the jsp file under WEB-INF directory. However, the jsp file fail to access the resources such as css, images, js .... meaning, the jsp page always fail to locate the resources. for example,

Comment

resources stored under /WEB-INF/ can not be accessed directely from the web agent. You should consider moving the images folder to the application root d:/proj/app/images/img1.gif and then use katentim code:

Code:

<img src="<c&#58;url value='/images/img1.gif' />" />

Comment

resources stored under /WEB-INF/ can not be accessed directely from the web agent. You should consider moving the images folder to the application root d:/proj/app/images/img1.gif and then use katentim code:

Code:

<img src="<c&#58;url value='/images/img1.gif' />" />

However, i saw some commercial framework, which use webmacro as view technology, have achieved this: all the images is stored under WEB-INF directory. And it can access the image just like this:

<img src="images/img1.gif" />

So, i just want to know how to achieve this using spring. Is it possible?

Comment

i saw some commercial framework, which use webmacro as view technology, have achieved this: all the images is stored under WEB-INF directory

If it worked, it is in violation of the spec. and won't be portable - the whole point of putting JSPs there is so they can't be accessed directly - forcing you to go via a controller (i.e. helping enforce MVC). Static content such as images and CSS MUST be accessed directly (unless you want to read them in from a servlet and stream them out).

Java™ Servlet Specification Version 2.3
SRV.9.5 Directory Structure

No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext.

Comment

i saw some commercial framework, which use webmacro as view technology, have achieved this: all the images is stored under WEB-INF directory

If it worked, it is in violation of the spec. and won't be portable - the whole point of putting JSPs there is so they can't be accessed directly - forcing you to go via a controller (i.e. helping enforce MVC). Static content such as images and CSS MUST be accessed directly (unless you want to read them in from a servlet and stream them out).

That's not quite true. It's quite possible to set up a filter (or a servlet for that matter) that is mapped with /images/* that forwards to /WEB-INF/images/ directory. While it's true that the browser can't directly access the /WEB-INF/ directory you can create code on the server to allow it. And yes it will work for images and CSS without violating the servlet spec.

/Scott

Comment

katentim talks about the "standard" behaviour, which means how a J2EE compliant Web Server should perform out of the box. Using Filters / Specific Servlets, you can, of course, do whatever you want, may be not everything...

I do not see, however, why you need to configure a filter to serve images / css from /WEB-INF/. Unless you have a real need, this is just putting more complexity into your architecture.

Comment

REAL need, may be it protect the images from downloading? What is meant by REAL need? Any example? just curious...

Besides protecting images from unauthorized use, another potential reason might be do deliver a modified css depending on the who the user is.

But I agree with Omar. Unless there's a need for it, it's probably best to just place them in a images or css directory at the root of web application. You can always create a filter later that protects the directories from direct access (even if they aren't in the WEB-INF directory).