slashnick
slashnick

Reputation: 26539

How can I create an absolute url in a jsp?

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?

Upvotes: 7

Views: 7720

Answers (1)

BalusC
BalusC

Reputation: 1108587

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:

Upvotes: 13

Related Questions