Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an absolute url in a jsp?

Tags:

url

jsp

jstl

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?

like image 925
slashnick Avatar asked Jun 28 '10 08:06

slashnick


People also ask

How do you use an absolute URL?

Linking with absolute URLs The domain is the name of the website. For example, the domain name of Indiana University's central web server is www.indiana.edu . The path includes directory and file information. You must use absolute URLs when referring to links on different servers.

What is an absolute URL?

What is an absolute URL? An absolute URL is the full URL, including protocol ( http / https ), the optional subdomain (e.g. www ), domain ( example.com ), and path (which includes the directory and slug). Absolute URLs provide all the available information to find the location of a page.

Which is an example of an absolute URL?

An absolute URL contains all the information necessary to locate a resource. In context to the Cart.com online stores system it begins with https://. For example, https://cart.com is an absolute URL.

How do I find the absolute URL?

An absolute URL contains the entire address from the protocol (HTTPS) to the domain name (www.example.com) and includes the location within your website in your folder system (/foldernameA or /foldernameB) names within the URL. Basically, it's the full URL of the page that you link to.


1 Answers

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)
like image 182
BalusC Avatar answered Sep 22 '22 21:09

BalusC