I understand that using a relative URL as the value for an HTML href attribute is usually considered A Good Thing To Do. However, is there a way to make relative URLs work reliably in combination with servlet "path mapping"? Here's a problem situation...
MyServlet produces an HTML page that uses a relative URL to locate a stylesheet, like this:
<link type="text/css" href="style/plain.css">
The web.xml for myapp uses path mapping, like this:
Eventually, I resorted to constructing an absolute URL for the stylesheet
The URL doesn't need to be absolute, but server-relative as I said above.
If you are doing this in a JSP, the <c:url> tag is your best friend. If you are doing this in a servlet.... well, why would you be generating markup in a servlet? [ February 07, 2006: Message edited by: Bear Bibeault ]
Joined: Feb 07, 2006
Thank you, Bear! The server-relative URL works great! And your explanation about the downside of page-relative URLs helped dispel my prejudice about them.
By the way, I encountered another, unrelated problem that sometimes prevented the stylesheet from taking effect. That problem was corrected by adding rel="StyleSheet" to my link element. (Thanks to Google and http://www.htmlhelp.com/reference/html40/head/link.html) Combining that fix with your fix, the working HTML now looks like this: