[ACCEPTED]-Java HttpServletRequest get URL in browsers URL bar-servlets

Accepted answer
Score: 43

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

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


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


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

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

Score: 9

Just did a slight tidy of the solution by 2 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 1 run a lot more efficiently than String comparisons.

Score: 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;


Score: 4

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

Score: 1

Can you try this



Score: 1

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

public String getUri(HttpServletRequest request) {
    String r = (String) request.getAttribute("javax.servlet.forward.request_uri");
    return r == null ? request.getRequestURI() : r;
Score: 0

Same answer as @kdgregory, but you can rather 1 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

More Related questions