32

How to get the base url from the jsp request object? http://localhost:8080/SOMETHING/index.jsp, but I want the part till index.jsp, how is it possible in jsp?

3
  • there are many ways. Why exactly do you need it?
    – Bozho
    Jun 7, 2011 at 20:20
  • I have some requirement for which I need the url of the web container. So, I need a path till localhost:8080/my_web_container.
    – Maverick
    Jun 7, 2011 at 20:22
  • this is now "web container", but "context". And do you need it absolute, or relative (to the host)
    – Bozho
    Jun 7, 2011 at 20:24

6 Answers 6

56

So, you want the base URL? You can get it in a servlet as follows:

String url = request.getRequestURL().toString();
String baseURL = url.substring(0, url.length() - request.getRequestURI().length()) + request.getContextPath() + "/";
// ...

Or in a JSP, as <base>, with little help of JSTL:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="req" value="${pageContext.request}" />
<c:set var="url">${req.requestURL}</c:set>
<c:set var="uri" value="${req.requestURI}" />
...
<head>
    <base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />
</head>

Note that this does not include the port number when it's already the default port number, such as 80. The java.net.URL doesn't take this into account.

See also:

1
  • what is the reason that setting the var for the "url" follows different syntax than setting the var for "req" and "uri" ? Dec 24, 2016 at 3:54
18

JSP variant of Bozho's answer:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="req" value="${pageContext.request}" />
<c:set var="baseURL" value="${req.scheme}://${req.serverName}:${req.serverPort}${req.contextPath}" />
1
  • ${pageContext.request.contextPath} yields nothing for /admin/home.
    – Chloe
    Feb 28, 2019 at 20:46
14
new URL(request.getScheme(), 
        request.getServerName(), 
        request.getServerPort(), 
        request.getContextPath());
2
  • 1
    what you you mean "till"? It will generate what you want, I think. Try it.
    – Bozho
    Jun 7, 2011 at 20:24
  • 2
    Note that this will even for default port numbers (http+80 and https+443) still include the port number in the final result. This is not always desireable, for sure not if the URL is to be used in some redirect URL or as <base> URL.
    – BalusC
    Jun 7, 2011 at 20:34
0

There is one major flaw in @BalusC accepted answer though. Substring should start from 0 and not 1. Instead of

<base href="${fn:replace(req.requestURL, fn:substring(uri, 1, fn:length(uri)), req.contextPath)}" />

it should be

<base href="${fn:replace(req.requestURL, fn:substring(uri, 0, fn:length(uri)), req.contextPath)}" />

With 1 you get double forward slash: http://localhost:8080//appcontext/ With 0 you get, http://localhost:21080/appcontext/

In my application, request.getSession(false) always returned null when it was ending in double slash!!!

0
-3

Instead of doing all of that, just do this:

request.getServerName().toString()
-3

just use isSecure()

{<%=request.isSecure()?"https":"http:"%>}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.