-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Uma,

Uma Kalluru wrote:
> <img src="../images/add.gif"/>
> 
> My index.jsp is stored in "jsp" folder. Here is the view of my context
> 
> /test
> jsp/index.jsp
> images/add.gif
> WEB-INF/
> ...
> 
> I am trying to use welcome-file and display this index.jsp page when the
> user requests for this context.
> 
>   <welcome-file-list>
>       <welcome-file>jsp/index.jsp</welcome-file>
>   </welcome-file-list>
> 
> When I invoke http://localhost:8080/test I am redirected to index.jsp but
> the image is not displayed.

Do you mean that index.jsp is served? I'm guessing that the URL in your
browser shows http://localhost:8080/test instead of
http://localhost:8080/test/jsp/index.jsp

If that's the case, then you have an incorrect URL in your index.jsp
page. You're trying to use a relative URL ("../images/add.gif") from a
page that does not have a fixed URL in the browser's eyes. The browser
sees the "current" path as "/test", so a relative URL of
"../images/add.gif" has a complete URL of
"http://localhost:8080/images/add.gif";, which is not where your image is.

It's good practice to use fully-qualified URLs (or, at least, full paths
rather than relative ones) whenever you are writing a webapp.

This is the brute-force way of doing it:

<img src="<%= application.getContextPath() %>/images/add.gif" />

If this were a dynamic resource (so instead of an image, it was a link
to another page), then you'd want to include the URL-encoded session id
in case cookies aren't being used. Then, you need this:

<img src="<% response.encodeURL(application.getContextPath()
             + "/images/add.gif") %> />

Gets messy, eh? That's why most people use JSP tag libraries to help out
with this stuff. If you're using JSTL (or have it available), you can
use the url tag like this:

<img src="<c:url value="/images/add.gif" />" />

This takes care of pre-pending the context path, re-writing the URL for
session management, and encoding any of the special characters you might
have used in your URL (like spaces, Unicode, or any punctuation
characters, etc.).

If you're not using JSTL, then look to see what is available to your
webapp and use that instead. I haven't used JSP in ages, so I have no
idea if Tomcat includes the JSTL out of the box.

Hope that helps,
- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHJzTM9CaO5/Lv0PARAvvIAJ9dop+9J+kLNbYDCF5lFguk1Q2IJACfdPGm
DylxKWIxxIXTK9vBEt/yues=
=pYhq
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to