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

查看:271
本文介绍了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.

Ex:\ AdvancedSearch:

getRequestURI()返回\ sububir \ search \ search.jsp

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

我正在寻找一种方法来获取浏览器看到的URL,而不是那个page knows只是一个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.

推荐答案

如果您当前的请求来自app-server内部或者包括,app-server应该将请求信息保存为请求属性。具体属性及其包含的内容取决于您是在进行转发还是包含。

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> ,原始父URL将由 request.getRequestURL()返回,有关所包含页面的信息将在以下请求属性中找到:

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()返回,原始请求的信息将在以下请求属性中找到:

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

这些内容见8.3节和Servlet 2.4规范的8.4。

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

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

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天全站免登陆