• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Context in tomcat

 
B Stokes
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65125
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
B Stokes
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

I went for the base tag and it's working with no problems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic