This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Mapping to local context root  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm a tomcat greenhorn and having some problems with setting up my apps.

Under my 'webapps' directory, I have three apps directories:

myApp
myOtherApp
ROOT

My problem is that when I create a page in the 'myApp' directory and put a link in it which should be relative to myApp's root, like:

<a href="/testpage.jsp">link</a>

when I click on the link in my browser, it goes to:

http://localhost:8080/testpage.jsp

instead of where I want it to go:

http://localhost:8080/myApp/testpage.jsp

Similarly, if I put the same page in the 'myOtherApp' directory, I want it to go to:

http://localhost:8080/myOtherApp/testpage.jsp

In other words, I want the links to be relative to the root of each app, not the root of webapps. I'm sure the answer's something simple but I've fiddled for hours and still got nowhere. I'm sure it's something to do with the context root, and I'm thinking I maybe have to put something in the web.xml of each app, or the main \conf\server.xml file? I can't work out what, though.

Any help is much appreciated.

Many thanks

D Taylor



 
Bartender
Posts: 4048
48
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use:
When used without the leading dot (".") it assumes the "ROOT" context (relative to the ROOT) which is why you got that result.
 
D Taylor
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks. That's awesome, that works!

But is there any way I can get my site to do that by default? The reason being I have a large web site to import into Tomcat, and there are literally thousands of links that need to work like that. I could do a find/replace to put a '.' in front of all the links that start with a slash, but I don't think that's a good way to do it...

Many thanks for your help,

D Taylor
 
Bartender
Posts: 19673
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that HTML has a "base" tag that can help. Struts has an extension of that tag that I think works better with J2EE. Otherwise you jave to do stuff like "#{pageContext.contextPath}/seller/newListing.jsp".

The RichFaces JSF template library also helps for stylesheets and scripts.

Relative URLs - especially relative to the app context root - are a major annoyance. However it's even more annoying to me to have hard-coded URLs, since it's a major pain to relocate them for testing, especially since I have a policy that the exact same WAR must be usable byte-for-byte in development, testing, and production.
 
D Taylor
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, cheers, I will look into that.
 
Author and ninkuma
Marshal
Posts: 66789
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D Taylor wrote:OK, thanks. That's awesome, that works!


It may work, but it's fragile and a poor practice.

You can either use the base tag as Tim suggested, or use server-relative addresses which always include the context path. The JSP FAQ shows how to easily do this without hard-coding it into the pages.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66789
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this is not Tomcat-specific, it's been moved to the JSP forum.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!