Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jsp - Java HttpServletRequest get URL in browsers URL bar

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() returns "subdirsearchsearch.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

     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:

     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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...