PDA

View Full Version : Access resources in WEB-INF


dev001
Nov 17th, 2004, 07: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,

<img src="<c:url value='/WEB-INF/images/img1.gif' />" />
<img src="images/img1.gif" />

This two tag return no images.

How to make the resources available in my web application?

katentim
Nov 17th, 2004, 09:17 PM
Try <img src="<c:url value='/images/img1.gif' />" />

If this doesn't work, just view your HTML source and check it is pointing to the correct location.

dev001
Nov 17th, 2004, 09:36 PM
<img src="<c:url value='/images/img1.gif' />" />

I try this and it return no image too. By viewing the html source, it is:
<img src='/app/images/img1.gif' />

Where 'app' is the web application root.

i store my image in d:/proj/app/WEB-INF/images/img1.gif.

Any idea?

irbouho
Nov 17th, 2004, 09:40 PM
i store my image in d:/proj/app/WEB-INF/images/img1.gif.
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:
<img src="<c:url value='/images/img1.gif' />" />

dev001
Nov 18th, 2004, 02:26 AM
i store my image in d:/proj/app/WEB-INF/images/img1.gif.
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:
<img src="<c: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?

katentim
Nov 18th, 2004, 04:22 AM
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.

scott69taylor
Nov 19th, 2004, 04:20 PM
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

irbouho
Nov 19th, 2004, 06:24 PM
Scott,

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.

dev001
Nov 20th, 2004, 02:15 AM
The commercial framework is Electronic Document Management System (EDMS), which used to manage scanned document (which is in jpeg, gif format).

So this framework allow to access the images in WEB-INF. And yes, they uses filter, after i have do a research to this framework and i found that.

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

scott69taylor
Nov 20th, 2004, 03:23 AM
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).

/Scott

dev001
Nov 22nd, 2004, 04:00 AM
Thanks for all the information provided!