You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Boris Folgmann <bo...@folgmann.de> on 2002/12/11 18:03:18 UTC

How to access /images?

Hi!

What's the preferred way of accessing e.g. images or other JSPs using
absolute pathnames in a JSP? If I use something like
<img src="/images/pic.gif"> this does not work if the webapp is not
deployed to the root context. There are some methods in ServletContext for
path handling, which should be used for constructing the right path?
I think it should look like that
<img src='<% magicthing("/images/pic.gif") %>'>

Does anybody know what the magicthing should be? At the moment I use
relative pathnames, but that gets complicated if you really have a lot of
JSPs, e.g. you need a lot of ../ if you have nested a lot of directories.

cu,
	boris
-- 
Dipl.-Inf. Boris Folgmann   mailto:boris@folgmann.de
Folgmann IT-Consulting      http://www.folgmann.de


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: How to access /images?

Posted by Ron Day <ro...@ronday.cc>.
nice solution...... is there a reason why you didn't make location a static
method ?

R

-----Original Message-----
From: micael [mailto:caraunltd@harbornet.com]
Sent: Wednesday, December 11, 2002 1:23 PM
To: Tomcat Users List
Subject: Re: How to access /images?


Don't know if this helps, but I put a Navigator class in
WEB-INF/classes/navigator/ and by accessing its ClassLoader can always tell
what the paths on the server are.  The sole purpose of this class is to
reveal information not capable of being known prior to startup (e.g. is
this Unix, XP, or Memorex?) and providing this to any part of the whole
deal that needs to know.  The parts can then use the information as they
like.  I usually create another class which gives me the location of the
application itself, which is stored in application scope and everything
from there is up to the individual parts to specify where they are relative
to that.

package navigator;

import java.io.File;
import java.net.URL;

public class Navigator {

     public URL location()
     {

         return
navigator.Navigator.class.getClassLoader().getResource("navigator" +
File.separator + "Navigator.class");

     }

}

At 11:26 AM 12/11/2002 -0600, you wrote:
>Hello Boris,
>
>This issue is much more problematic in servlets.  Since you know which
>directory your .jsp file is relative to your images, I would continue
>using relative paths.  This is what you'd need to do for static html
>files anyway.  For servlets, prepending the context path is probably a
>good idea, but make sure to ask the servlet api what the current
>context path is instead of just assuming your app will always be
>called, for instance, "myapp".  The name could change or it could be
>served off the root context where the context path would be "/" so
>always grab that value dynamically.
>
>Otherwise, use an MVC pattern and access a servlet via a mapping to
>your controller at the root of the context and *only* at the root of
>the context.  That way, you always know where your static resources
>exist relative to the current dynamic page.
>
>Otherwise, you can always pre-pend all static resources with some path
>which maps to a servlet which serves resources out of a classloader.
>You could put your images in a .jar file in some package.  that way,
>no matter where your page is, you always know exactly how to refer to
>the resources since the package name never changes.  The Barracuda
>project has such a servlet called ResourceGateway which you can take a
>look at here.
>http://barracuda.enhydra.org/software/cvs/cvsweb.cgi/Projects/EnhydraOrg/to
olsTech/Barracuda/src/org/enhydra/barracuda/core/helper/servlet/ResourceGate
way.java
>
>Jake
>
>Wednesday, December 11, 2002, 11:03:18 AM, you wrote:
>
>BF> Hi!
>
>BF> What's the preferred way of accessing e.g. images or other JSPs using
>BF> absolute pathnames in a JSP? If I use something like
>BF> <img src="/images/pic.gif"> this does not work if the webapp is not
>BF> deployed to the root context. There are some methods in ServletContext
for
>BF> path handling, which should be used for constructing the right path?
>BF> I think it should look like that
>BF> <img src='<% magicthing("/images/pic.gif") %>'>
>
>BF> Does anybody know what the magicthing should be? At the moment I use
>BF> relative pathnames, but that gets complicated if you really have a lot
of
>BF> JSPs, e.g. you need a lot of ../ if you have nested a lot of
directories.
>
>BF> cu,
>BF>         boris
>
>
>
>--
>Best regards,
>  Jacob                            mailto:hoju@visi.com
>
>
>--
>To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
>For additional commands, e-mail:
<ma...@jakarta.apache.org>

Micael

-------------------------------------------------------

This electronic mail  transmission and any accompanying documents contain
information belonging to the sender which may be confidential and legally
privileged.  This information is intended only for the use of the
individual or entity to whom this electronic mail transmission was sent as
indicated above. If you are not the intended recipient, any disclosure,
copying, distribution, or action taken in reliance on the contents of the
information contained in this transmission is strictly prohibited.  If you
have received this transmission in error, please delete the message.  Thank
you



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to access /images?

Posted by micael <ca...@harbornet.com>.
Don't know if this helps, but I put a Navigator class in 
WEB-INF/classes/navigator/ and by accessing its ClassLoader can always tell 
what the paths on the server are.  The sole purpose of this class is to 
reveal information not capable of being known prior to startup (e.g. is 
this Unix, XP, or Memorex?) and providing this to any part of the whole 
deal that needs to know.  The parts can then use the information as they 
like.  I usually create another class which gives me the location of the 
application itself, which is stored in application scope and everything 
from there is up to the individual parts to specify where they are relative 
to that.

package navigator;

import java.io.File;
import java.net.URL;

public class Navigator {

     public URL location()
     {

         return 
navigator.Navigator.class.getClassLoader().getResource("navigator" + 
File.separator + "Navigator.class");

     }

}

At 11:26 AM 12/11/2002 -0600, you wrote:
>Hello Boris,
>
>This issue is much more problematic in servlets.  Since you know which
>directory your .jsp file is relative to your images, I would continue
>using relative paths.  This is what you'd need to do for static html
>files anyway.  For servlets, prepending the context path is probably a
>good idea, but make sure to ask the servlet api what the current
>context path is instead of just assuming your app will always be
>called, for instance, "myapp".  The name could change or it could be
>served off the root context where the context path would be "/" so
>always grab that value dynamically.
>
>Otherwise, use an MVC pattern and access a servlet via a mapping to
>your controller at the root of the context and *only* at the root of
>the context.  That way, you always know where your static resources
>exist relative to the current dynamic page.
>
>Otherwise, you can always pre-pend all static resources with some path
>which maps to a servlet which serves resources out of a classloader.
>You could put your images in a .jar file in some package.  that way,
>no matter where your page is, you always know exactly how to refer to
>the resources since the package name never changes.  The Barracuda
>project has such a servlet called ResourceGateway which you can take a
>look at here.
>http://barracuda.enhydra.org/software/cvs/cvsweb.cgi/Projects/EnhydraOrg/toolsTech/Barracuda/src/org/enhydra/barracuda/core/helper/servlet/ResourceGateway.java
>
>Jake
>
>Wednesday, December 11, 2002, 11:03:18 AM, you wrote:
>
>BF> Hi!
>
>BF> What's the preferred way of accessing e.g. images or other JSPs using
>BF> absolute pathnames in a JSP? If I use something like
>BF> <img src="/images/pic.gif"> this does not work if the webapp is not
>BF> deployed to the root context. There are some methods in ServletContext for
>BF> path handling, which should be used for constructing the right path?
>BF> I think it should look like that
>BF> <img src='<% magicthing("/images/pic.gif") %>'>
>
>BF> Does anybody know what the magicthing should be? At the moment I use
>BF> relative pathnames, but that gets complicated if you really have a lot of
>BF> JSPs, e.g. you need a lot of ../ if you have nested a lot of directories.
>
>BF> cu,
>BF>         boris
>
>
>
>--
>Best regards,
>  Jacob                            mailto:hoju@visi.com
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>

Micael

-------------------------------------------------------

This electronic mail  transmission and any accompanying documents contain 
information belonging to the sender which may be confidential and legally 
privileged.  This information is intended only for the use of the 
individual or entity to whom this electronic mail transmission was sent as 
indicated above. If you are not the intended recipient, any disclosure, 
copying, distribution, or action taken in reliance on the contents of the 
information contained in this transmission is strictly prohibited.  If you 
have received this transmission in error, please delete the message.  Thank 
you  



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to access /images?

Posted by Jacob Kjome <ho...@visi.com>.
Hello Boris,

This issue is much more problematic in servlets.  Since you know which
directory your .jsp file is relative to your images, I would continue
using relative paths.  This is what you'd need to do for static html
files anyway.  For servlets, prepending the context path is probably a
good idea, but make sure to ask the servlet api what the current
context path is instead of just assuming your app will always be
called, for instance, "myapp".  The name could change or it could be
served off the root context where the context path would be "/" so
always grab that value dynamically.

Otherwise, use an MVC pattern and access a servlet via a mapping to
your controller at the root of the context and *only* at the root of
the context.  That way, you always know where your static resources
exist relative to the current dynamic page.

Otherwise, you can always pre-pend all static resources with some path
which maps to a servlet which serves resources out of a classloader.
You could put your images in a .jar file in some package.  that way,
no matter where your page is, you always know exactly how to refer to
the resources since the package name never changes.  The Barracuda
project has such a servlet called ResourceGateway which you can take a
look at here.
http://barracuda.enhydra.org/software/cvs/cvsweb.cgi/Projects/EnhydraOrg/toolsTech/Barracuda/src/org/enhydra/barracuda/core/helper/servlet/ResourceGateway.java

Jake

Wednesday, December 11, 2002, 11:03:18 AM, you wrote:

BF> Hi!

BF> What's the preferred way of accessing e.g. images or other JSPs using
BF> absolute pathnames in a JSP? If I use something like
BF> <img src="/images/pic.gif"> this does not work if the webapp is not
BF> deployed to the root context. There are some methods in ServletContext for
BF> path handling, which should be used for constructing the right path?
BF> I think it should look like that
BF> <img src='<% magicthing("/images/pic.gif") %>'>

BF> Does anybody know what the magicthing should be? At the moment I use
BF> relative pathnames, but that gets complicated if you really have a lot of
BF> JSPs, e.g. you need a lot of ../ if you have nested a lot of directories.

BF> cu,
BF>         boris



-- 
Best regards,
 Jacob                            mailto:hoju@visi.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to access /images?

Posted by Boris Folgmann <bo...@folgmann.de>.
Noel J. Bergman wrote:
> Either of
> 
>   <img src='<%= request.getContextPath() + "/images/myimage.jpg" %>'>
>   <img src='<%= request.getContextPath()%>/images/myimage.jpg'>

Thank you very much. That's excatly what I need.

BTW: If you use struts <html:link> seems to do this automatically. Anyway,
I've got a large project w/o struts!

cu,
	boris
-- 
Dipl.-Inf. Boris Folgmann   mailto:boris@folgmann.de
Folgmann IT-Consulting      http://www.folgmann.de


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: How to access /images?

Posted by "Noel J. Bergman" <no...@devtech.com>.
Either of

  <img src='<%= request.getContextPath() + "/images/myimage.jpg" %>'>
  <img src='<%= request.getContextPath()%>/images/myimage.jpg'>

depending upon your stylistic preference.

	--- Noel

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>