File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Context Root in Tomcat 4.1.18 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Context Root in Tomcat 4.1.18" Watch "Context Root in Tomcat 4.1.18" New topic

Context Root in Tomcat 4.1.18

Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
Hi there!
I've got a web-application that I use the jsp:include tag to manipulate a header throught my pages. The problem is that the jsp files aren't in the same directory, so the images loaded from header.jsp got different paths and throws broken links.
I tried to use the "/" character to solve this, but instead getting the root from context root, the path that returns is from the host.
<img src="/images/main_banner.gif" width="750" height="70">
path = http://myhost/images/main_banner.gif
I want to get http://myhost/mycontext/images/main_banner.gif
Any idea?

Tiago Fernandez
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

That's right. References from tags such as img, script, link and so on are server-relative, so you'll need to include the context path in the URLs.
If you don't want to hard-code the context path (and I suggest you don't), you can pick it up easily with request.getContextPath().

[Asking smart questions] [About Bear] [Books by Bear]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
<img src="<%= request.getContextPath() %>/images/main_banner.gif">
Not very pretty, and I'd love to know if there's a good way to do this without a scriptlet expression.
Your life would be simpler if all of your JSPs were in the same directory, so you could use a relative URL (one that doesn't begin with "/").

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I like to use the "base" HTML tag to specify where images, sounds, css etc come from.
Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
Thank you all!
Now I'm using the base html tag and the request.getContextPath() to solve this problem, but I don't know why my context path called "Saude" from the host "tiago" that I get is not correct.
<base href="<%= request.getContextPath() %>">
<img src="images/main_banner.gif">
Path (incorrect):
If I do this:
<img src="<%= request.getContextPath() %>/images/main_banner.gif">
I get this path (incorrect too):
What's going on?
Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
Well, I found out myself a solution.
Now I'm using a context parameter in my web.xml:
And in my JSP pages:
<base href="<%= getServletContext().getInitParameter("contextPath")%>">
That's it...
I agree. Here's the link:
subject: Context Root in Tomcat 4.1.18
It's not a secret anymore!