24

So I'm trying to grab the current URL of the page using Java's request object. I've been using request.getRequestURI() to preform this, but I noticed that when a java class reroutes me to a different page off a servlet request getRequestURI gives that that address as opposed to the orginal URL that was typed in the browser and which still shows in the browser.

Ex: \AdvancedSearch:
getRequestURI() returns "\subdir\search\search.jsp"

I'm looking for a way to grab what the browser sees as the URL and not what that page knows is only a servlet wrapper.

Ballsacian1
  • 17,214
  • 2
  • 26
  • 25
  • The answer in the link below will definitely help someone. [http://stackoverflow.com/questions/8024344/user-login-with-jsf-2-0/39379256#39379256](http://stackoverflow.com/questions/8024344/user-login-with-jsf-2-0/39379256#39379256) – Tunde Michael Sep 07 '16 at 21:31

7 Answers7

43

If your current request is coming from an "inside the app-server" forward or include, the app-server is expected to preserve request information as request attributes. The specific attributes, and what they contain, depends on whether you're doing a forward or an include.

For <jsp:include>, the original parent URL will be returned by request.getRequestURL(), and information about the included page will be found in the following request attributes:

     javax.servlet.include.request_uri
     javax.servlet.include.context_path
     javax.servlet.include.servlet_path
     javax.servlet.include.path_info
     javax.servlet.include.query_string

For <jsp:forward>, the new URL will be returned by request.getRequestURL(), and the original request's information will be found in the following request attributes:

     javax.servlet.forward.request_uri
     javax.servlet.forward.context_path
     javax.servlet.forward.servlet_path
     javax.servlet.forward.path_info
     javax.servlet.forward.query_string

These are set out in section 8.3 and 8.4 of the Servlet 2.4 specification.

However, be aware that this information is only preserved for internally-dispatched requests. If you have a front-end web-server, or dispatch outside of the current container, these values will be null. In other words, you may have no way to find the original request URL.

kdgregory
  • 38,754
  • 10
  • 77
  • 102
  • I tried out.println(request.getAttribute("request_uri")); out.println(request.getAttribute("context_path")); out.println(request.getAttribute("servlet_path")); out.println(request.getAttribute("path_info")); out.println(request.getAttribute("query_string")); All returned null. Does this mean I'm screwed? – Ballsacian1 Aug 10 '09 at 19:12
  • 4
    The actual attribute name is "javax.servlet.include.request_uri", not "request_uri" – kdgregory Aug 10 '09 at 19:16
  • Thanks that works. Need to add a little more logic in my navigation, but hey saves me a lot of trouble. Thanks. – Ballsacian1 Aug 10 '09 at 19:49
9

Just did a slight tidy of the solution by Ballsacian1

String currentURL = null;
if( request.getAttribute("javax.servlet.forward.request_uri") != null ){
    currentURL = (String)request.getAttribute("javax.servlet.forward.request_uri");
}
if( currentURL != null && request.getAttribute("javax.servlet.include.query_string") != null ){
    currentURL += "?" + request.getQueryString();
}

The null checks are going to run a lot more efficiently than String comparisons.

readikus
  • 369
  • 1
  • 6
  • 17
  • Thanks for this. While not currently of much use to me, This is one of those questions that I'm sure will come back to me in the future. – Ballsacian1 Sep 05 '12 at 02:18
  • @readikus - should that be `javax.servlet.forward.query_string` instead of `javax.servlet.include.query_string`? – Stephen Rudolph Jul 05 '17 at 17:18
5
String activePage = "";
    // using getAttribute allows us to get the orginal url out of the page when a forward has taken place.
    String queryString = "?"+request.getAttribute("javax.servlet.forward.query_string");
    String requestURI = ""+request.getAttribute("javax.servlet.forward.request_uri");
    if(requestURI == "null") {
        // using getAttribute allows us to get the orginal url out of the page when a include has taken place.
        queryString = "?"+request.getAttribute("javax.servlet.include.query_string");
        requestURI = ""+request.getAttribute("javax.servlet.include.request_uri");
    }
    if(requestURI == "null") {
        queryString = "?"+request.getQueryString();
        requestURI = request.getRequestURI();
    }
    if(queryString.equals("?null")) queryString = "";
    activePage = requestURI+queryString;
Ballsacian1
  • 17,214
  • 2
  • 26
  • 25
4

${requestScope['javax.servlet.forward.query_string']} -- if you access it form jsp, using Expression Language

MihaiS
  • 191
  • 1
  • 5
1

Can you try this

<%=request.getRequestURL().toString()%>
iCrazybest
  • 2,935
  • 2
  • 24
  • 24
1

To get the HTTP requested path without know the state of the internal flow of the request, use this method:

public String getUri(HttpServletRequest request) {
    String r = (String) request.getAttribute("javax.servlet.forward.request_uri");
    return r == null ? request.getRequestURI() : r;
}
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
0

Same answer as @kdgregory, but you can rather use the Request Dispatcher constants.

javax.servlet.include.request_uri        RequestDispatcher.FORWARD_REQUEST_URI
javax.servlet.include.context_path       RequestDispatcher.FORWARD_CONTEXT_PATH
javax.servlet.include.servlet_path       RequestDispatcher.FORWARD_SERVLET_PATH
javax.servlet.include.path_info          RequestDispatcher.FORWARD_PATH_INFO
javax.servlet.include.query_string       RequestDispatcher.FORWARD_QUERY_STRING
fingerprints
  • 2,751
  • 1
  • 25
  • 45