Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Access resources in WEB-INF

  1. #1
    Join Date
    Nov 2004
    Posts
    7

    Default Access resources in WEB-INF

    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?

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Try
    Code:
    <img src="<c&#58;url value='/images/img1.gif' />" />
    If this doesn't work, just view your HTML source and check it is pointing to the correct location.

  3. #3
    Join Date
    Nov 2004
    Posts
    7

    Default

    <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?

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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:
    Code:
    <img src="<c&#58;url value='/images/img1.gif' />" />
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Nov 2004
    Posts
    7

    Default

    Quote Originally Posted by irbouho
    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:
    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?

  6. #6
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

  7. #7

    Default

    Quote Originally Posted by katentim
    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

  8. #8
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  9. #9
    Join Date
    Nov 2004
    Posts
    7

    Default REAL need

    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...

  10. #10

    Default Re: REAL need

    Quote Originally Posted by dev001
    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

Similar Threads

  1. Replies: 2
    Last Post: Sep 14th, 2008, 09:21 PM
  2. Replies: 2
    Last Post: Jul 21st, 2005, 06:09 PM
  3. Access to model
    By maveganzones in forum Web
    Replies: 6
    Last Post: Jul 8th, 2005, 01:00 PM
  4. Replies: 0
    Last Post: Jun 10th, 2005, 05:44 AM
  5. Replies: 2
    Last Post: Nov 1st, 2004, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •