• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Context Root in Tomcat 4.1.18

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "/").
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic