Java HttpServletRequest 在浏览器 URL 栏中获取 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1256562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:30:31  来源:igfitidea点击:

Java HttpServletRequest get URL in browsers URL bar

javajspurlservlets

提问by Ballsacian1

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.

所以我试图使用 Java 的请求对象获取页面的当前 URL。我一直在使用 request.getRequestURI() 来执行此操作,但我注意到当 Java 类将我重新路由到 servlet 请求的不同页面时,getRequestURI 会提供该地址,而不是在浏览器中键入的原始 URL 和它仍然显示在浏览器中。

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

例如:\AdvancedSearch:
getRequestURI()返回“\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.

我正在寻找一种方法来获取浏览器视为 URL 的内容,而不是该页面所知道的只是一个 servlet 包装器。

采纳答案by kdgregory

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:

对于<jsp:include>,原始父 URL 将由 返回request.getRequestURL(),有关包含页面的信息将在以下请求属性中找到:

     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:

对于<jsp:forward>,新的 URL 将由 返回request.getRequestURL(),原始请求的信息将在以下请求属性中找到:

     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.

这些在 Servlet 2.4 规范的第 8.3 和 8.4 节中列出。

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.

但是,请注意,此信息仅保留用于内部调度的请求。如果您有前端 Web 服务器,或在当前容器之外调度,则这些值将为空。换句话说,您可能无法找到原始请求 URL。

回答by Ballsacian1

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;

回答by MihaiS

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

${requestScope['javax.servlet.forward.query_string']} -- 如果你从jsp访问它,使用表达式语言

回答by readikus

Just did a slight tidy of the solution by Ballsacian1

刚刚对 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.

空检查将比字符串比较更有效地运行。

回答by iCrazybest

Can you try this

你能试试这个吗

<%=request.getRequestURL().toString()%>

回答by Daniel De León

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

要在不知道请求内部流状态的情况下获取 HTTP 请求路径,请使用此方法:

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

回答by winter

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

与@kdgregory 的答案相同,但您可以使用 Request Dispatcher 常量。

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