Java HttpServletRequest get URL in browsers URL bar

后端 未结 7 706
旧巷少年郎
旧巷少年郎 2020-11-27 14:03

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 cla

7条回答
  •  天命终不由人
    2020-11-27 14:59

    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.

提交回复
热议问题