GeeCON Prague 2014*
The moose likes JSP and the fly likes Mapping to local context root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Mapping to local context root" Watch "Mapping to local context root" New topic
Author

Mapping to local context root

D Taylor
Greenhorn

Joined: Jun 10, 2008
Posts: 8
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

Joined: Mar 24, 2008
Posts: 3833

Use:
When used without the leading dot (".") it assumes the "ROOT" context (relative to the ROOT) which is why you got that result.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
D Taylor
Greenhorn

Joined: Jun 10, 2008
Posts: 8
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

Joined: Jun 25, 2001
Posts: 16095
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
D Taylor
Greenhorn

Joined: Jun 10, 2008
Posts: 8
OK, cheers, I will look into that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

As this is not Tomcat-specific, it's been moved to the JSP forum.
 
GeeCON Prague 2014
 
subject: Mapping to local context root