43

How can I get request URL in JSP which is forwarded by Servlet?

If I run following code in JSP,

System.out.println("servlet path= " + request.getServletPath());
System.out.println("request URL= " + request.getRequestURL());
System.out.println("request URI= " + request.getRequestURI());

then I get the server side path to the JSP. But I want to get the URL as you can see in browser's address bar. I can get it in the Servlet that forwards to the JSP, but I want to get it within the JSP.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user12384512
  • 3,362
  • 10
  • 61
  • 97

8 Answers8

68

If you use RequestDispatcher.forward() to route the request from controller to the view, then request URI is exposed as a request attribute named javax.servlet.forward.request_uri. So, you can use

request.getAttribute("javax.servlet.forward.request_uri")

or

${requestScope['javax.servlet.forward.request_uri']}
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • This is not url of forwarded view, this is a part of url in address bar. I can get url of forwarded view by request.getRequestURI() – user12384512 Jun 08 '10 at 14:15
  • 3
    @BalusC: He is completely right. When he forwards request to the view (using `RequestDispatcher.forward()`), request URI (returned by `getRequestURI()`) is replaced with the view URI. Original URI (as entered in the address bar) is saved as attribute. – axtavt Jun 08 '10 at 15:08
  • 5
    In case anyone else stumbles upon this and is trying to rebuild the the forward.request_uri and its accompanying attributes (query string) you also need `request.getAttribute("javax.servlet.forward.query_string")` Good answer. Thanks +1 – matchew Nov 30 '12 at 17:20
  • 1
    `req.getAttribute("javax.servlet.forward.request_uri")` work only in JSP. if you run this in controller before JSP then result is `null` – Koss May 17 '13 at 06:12
  • i redirected when the page was accessed using `.jsp` by the condition `if (request.getAttribute("javax.servlet.forward.request_uri") == null && request.getRequestURI().endsWith("index.jsp"))` – Stalin Gino Dec 13 '14 at 14:34
9

Try this instead:

String scheme = req.getScheme();             
String serverName = req.getServerName(); 
int serverPort = req.getServerPort();    
String uri = (String) req.getAttribute("javax.servlet.forward.request_uri");
String prmstr = (String) req.getAttribute("javax.servlet.forward.query_string");
String url = scheme + "://" +serverName + ":" + serverPort + uri + "?" + prmstr;

Note: You can't get HREF anchor from your url. Example, if you have url "toc.html#top" then you can get only "toc.html"

Note: req.getAttribute("javax.servlet.forward.request_uri") work only in JSP. if you run this in controller before JSP then result is null

You can use code for both variant:

public static String getCurrentUrl(HttpServletRequest req) {
    String url = getCurrentUrlWithoutParams(req);
    String prmstr = getCurrentUrlParams(req);
    url += "?" + prmstr;
    return url;
}

public static String getCurrentUrlParams(HttpServletRequest request) {
    return StringUtil.safeString(request.getQueryString());
}

public static String getCurrentUrlWithoutParams(HttpServletRequest request) {
    String uri = (String) request.getAttribute("javax.servlet.forward.request_uri");
    if (uri == null) {
        return request.getRequestURL().toString();
    }
    String scheme = request.getScheme();
    String serverName = request.getServerName();
    int serverPort = request.getServerPort();
    String url = scheme + "://" + serverName + ":" + serverPort + uri;
    return url;
}
Koss
  • 1,012
  • 11
  • 8
  • 1
    **Note:** `req.getAttribute("javax.servlet.forward.request_uri")` work only in JSP. if you run this in controller before JSP then result is `null` – Koss May 17 '13 at 05:41
8

To get the current path from within the JSP file you can simply do one of the following:

<%= request.getContextPath() %>
<%= request.getRequestURI() %>
<%= request.getRequestURL() %>
lanoxx
  • 12,249
  • 13
  • 87
  • 142
5

Try this,

<c:set var="pageUrl" scope="request">
    <c:out value="${pageContext.request.scheme}://${pageContext.request.serverName}"/>
    <c:if test="${pageContext.request.serverPort != '80'}">
        <c:out value=":${pageContext.request.serverPort}"/>
    </c:if>
    <c:out value="${requestScope['javax.servlet.forward.request_uri']}"/>
</c:set>

I would like to put it in my base template and use in whole app whenever i need to.

Hasasn
  • 810
  • 8
  • 9
4

Also you could use

${pageContext.request.requestURI}
divideByZero
  • 1,120
  • 16
  • 27
4

None of these attributes are reliable because per the servlet spec (2.4, 2.5 and 3.0), these attributes are overridden if you include/forward a second time (or if someone calls getNamedDispatcher). I think the only reliable way to get the original request URI/query string is to stick a filter at the beginning of your filter chain in web.xml that sets your own custom request attributes based on request.getRequestURI()/getQueryString() before any forwards/includes take place.

http://www.caucho.com/resin-3.0/webapp/faq.xtp contains an excellent summary of how this works (minus the technical note that a second forward/include messes up your ability to use these attributes).

3

To avoid using scriplets in the jsp, follow the advice of "divideByZero", and use ${pageContext.request.requestURI} This is a better way to go.

stock
  • 117
  • 5
  • 3
    How is this any different from the older answer you are referencing? – jbindel Feb 27 '14 at 22:17
  • The answer is no different in what to do. I added some clarification that by using ${pageContext.request.requestURI} you can avoid using scriplets. – stock Mar 13 '14 at 21:56
0

Same as @axtavt, but you can use also the RequestDispatcher constant.

request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
fingerprints
  • 2,751
  • 1
  • 25
  • 45