Java HttpServletRequest 在浏览器 URL 栏中获取 URL [英] Java HttpServletRequest get URL in browsers URL bar

查看:35
本文介绍了Java HttpServletRequest 在浏览器 URL 栏中获取 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如:AdvancedSearch:
getRequestURI() 返回subdirsearchsearch.jsp"

Ex: AdvancedSearch:
getRequestURI() returns "subdirsearchsearch.jsp"

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

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.

推荐答案

如果您当前的请求来自应用服务器内部"转发或包含,则应用服务器应保留请求信息作为请求属性.具体属性及其包含的内容取决于您是在执行转发还是包含.

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.

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

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

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

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

这些在 Servlet 2.4 规范的第 8.3 和 8.4 节中有规定.

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

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

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.

这篇关于Java HttpServletRequest 在浏览器 URL 栏中获取 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆