Q:

Cannot use relative links in my tomcat development environment

I'm developing a web-application with tomcat. In the dev env, the application's root address is localhost:8080/myapp/. In production, the root address is www.myapp.com/.
When I use a relative link in my website, e.g. /home, it works fine in production, but the address that I arrive at in my dev is localhost:8080/home, which strips the application's root. Obviously, I cannot just use home, as it would work the first time, but would just concatenate to the end of the address string. I also cannot use the hosts file, and I have no idea how to create an alias or just redirect traffic from some alias to localhost:8080/myapp/.
This seems like a very common issue. Thanks in advance!

A:

The solution is actually in the code of your web pages and not in any configuration that you can do. Presuming that you are using JSP to generate your HTML, you need to change links like this:
<a href="/home">a link to the root of my webapp</a>

To look like this:
<a href="<%= response.encodeURL(request.getContextPath() + "/home") %>">
  a link to the root of my webapp
</a>

The request.getContextPath is the real magic here: it always returns the prefix that belongs to your webapp, no matter what it is. For the ROOT webapp, it will return "" (empty string). For another it might return "/foo/bar" (if your webapp were deployed as foo#bar.war).
The response.encodeURL will add the session identifier to your URL if the client is not using cookies so that sessions will work when cookies are not available.
If you are using some other content-generation framework (Velocity, Freemarker, etc.) there are analogous ways of achieving the same thing provided by those frameworks.

