wood burning stoves 2.0*
The moose likes Tomcat and the fly likes Context in tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Context in tomcat" Watch "Context in tomcat" New topic
Author

Context in tomcat

B Stokes
Greenhorn

Joined: Aug 04, 2004
Posts: 9
I'm developing a web app. in Netbeans 3.6 using the inbuilt tomcat 5.x.

I'm having difficulty referring to other resources (pages/images/...) in the root of the context. I have directories in the root folder such as JSP,images,... to hold the different resources but want to be able to write a single non-relative link to access them.

i.e. in any webpage I want to write ...src="/images/myimage.gif"...

Unfortunately that takes me back to the very root of tomcat and not my context (http://localhost:8080/images instead of what I want which is http://localhost:8080/myapp/images)

Is there an easy way of doing this without putting src="<%=request.getContextPath()%>/images/myimage.gif" ?

Thanks in advance.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

JSTL has the <c:url> tag. Struts has the <html:link page=""> construct. As far as automagically resolving the context, no such luck, but relative links work as long as you pay attention to what you're being relative to.

Ex:

&llt;img src="../images/myimage.gif">

The "double-dot" convention means the same as it does for Unix/Linux and Windows relative file paths, except that the relativity is to webserver context paths. Of course, a context path isn't always translated a file path, which is where you can get bitten!


Customer surveys are for companies who didn't pay proper attention to begin with.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Is there an easy way of doing this without putting src="<%=request.getContextPath()%>/images/myimage.gif" ?


JSP 2.0 syntax is a little easier to read:



You could also investigate the use of the <base> HTML tag to set a relative path to your context root:



Personally I would avoid relative addressing using .. and the like -- it requires assumptions about the files relative position to each other, introducing uneccessary implicit knowledge binding, and makes it hard to rearrange the folder hierarchy.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
B Stokes
Greenhorn

Joined: Aug 04, 2004
Posts: 9
Thanks!

I went for the base tag and it's working with no problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Context in tomcat
 
Similar Threads
Tomcat url redirect problem
How to setup the same application (multiple instances) running on different ports under Tomcat 6.0.x
comparision between loading images from servlet vs. from files
Mapping to local context root
serve static HTML from app context rather than ROOT