• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Context Root in Tomcat 4.1.18

 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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 "/").
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to use the "base" HTML tag to specify where images, sounds, css etc come from.
 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic