This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping to local context root

 
D Taylor
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



 
Vijitha Kumara
Bartender
Posts: 3913
9
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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17987
47
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
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
Pie
Posts: 64606
86
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic