If a have <c:url value="/article"/>
in a jsp, I actually want it to produce http://mysite.com/context/article
. Is there any simple way to acheive this?
views:
38answers:
3
A:
Yes, use plain html:
<a href="http://mysite.com">click</a>
If you want to use the current site's url, just use request.getServerName()
, or in jstl - ${request.serverName}
Bozho
2010-06-28 08:50:00
I don't want to hard code the domain as the jsp is used on many sites.
slashnick
2010-06-28 08:59:33
you could've included that detail in the question ;)
Bozho
2010-06-28 10:15:54
Note: the `ServletRequest#getServerName()` only returns the hostname or IP. Not protocol/port.
BalusC
2010-06-28 12:16:34
No despite the name that shows how to create a url relative to the context root, which does not include the domain.
slashnick
2010-06-28 08:59:05
Ah - too bad, I didn't realize that. You can use request.getServerName() to get the servername - so if you know the protocol you should be fine with the combination of that and the linked question.
Simon Groenewolt
2010-06-28 13:28:06
A:
There's no simple way. Either hardcode it or output the following:
${fn:replace(pageContext.request.requestURL, pageContext.request.requestURI, '')}${pageContext.request.contextPath}
Cumbersome, but there's no shorter/nicer way when you want to take the protocol and port parts of the URL correctly into account. You can at highest assign ${pageContext.request}
to ${r}
.
<c:set var="r" value="${pageContext.request}" />
so that you can end up with this
${fn:replace(r.requestURL, r.requestURI, '')}${r.contextPath}
That said, I only fail to see how this requirement is useful/valuable. I always code my webapp-specific links to be relative to the current context or to the HTML <base>
tag. Otherwise you'll have to a lot of maintenance when your domain, port and/or even the context changes. Why this requirement?
See also:
- Is it recommended to use the HTML
<base>
tag? (sensitive subject actually)
BalusC
2010-06-28 11:37:26