aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Context Root in Tomcat 4.1.18" Watch "Context Root in Tomcat 4.1.18" New topic
Author

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.
Example:
<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?
Thanks!


Tiago Fernandez
http://www.tiago182.spyw.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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().
hth,
bear


[Asking smart questions] [Bear's FrontMan] [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
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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.
Example:
<base href="<%= request.getContextPath() %>">
<img src="images/main_banner.gif">
Path (incorrect):
http:///imagens/main_banner.gif
If I do this:
<img src="<%= request.getContextPath() %>/images/main_banner.gif">
I get this path (incorrect too):
http:///Saude/images/main_banner.gif
What's going on?
Tks
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:
<context-param>
<param-name>contextPath</param-name>
<param-value>http://tiago/Saude/</param-value>
</context-param>
And in my JSP pages:
<base href="<%= getServletContext().getInitParameter("contextPath")%>">
That's it...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Context Root in Tomcat 4.1.18