Java 如何从JSP中的地址栏获取URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23804236/
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
how to get URL from address bar in JSP
提问by Vivek
Lets say I hit
可以说我打了
http://localhost/webapp/wcs/stores/servlet/en/marksandspencer/l/women/dresses/party-and-cocktail-dresses
http://localhost/webapp/wcs/stores/servlet/en/marksandspencer/l/women/dresses/party-and-cocktail-dresses
and this internally redirects me to custom 404.jsp page, But URL remain same in address bar.
这在内部将我重定向到自定义 404.jsp 页面,但地址栏中的 URL 保持不变。
I tried this code - <%= request.getAttribute("javax.servlet.forward.request_uri") %>;
and it's returning me the path of 404.jsp
我试过这段代码 -<%= request.getAttribute("javax.servlet.forward.request_uri") %>;
它返回给我 404.jsp 的路径
How can I get the entered URL which is there in address bar?
如何获取地址栏中输入的URL?
采纳答案by mindas
I think you were close. javax.servlet.forward.request_uri
is for normal forwarding, but for 404, you need javax.servlet.error.request_uri
.
我认为你很接近。javax.servlet.forward.request_uri
用于正常转发,但对于 404,您需要javax.servlet.error.request_uri
.
回答by Yogesh Prajapati
use request.getHeader("Referer")
.
使用request.getHeader("Referer")
.
referer gives a url from where you redirected.
referer 给出了你重定向的网址。
回答by Ankit Lamba
You can use :
您可以使用 :
String url = request.getRequestURL().toString();
but this doesn't hold Query String. So, to get query string, you may call
但这不包含查询字符串。因此,要获取查询字符串,您可以调用
request.getQueryString()
回答by Loc
Use request.getAttribute("javax.servlet.error.request_uri")to get URI of requested page that not found (404 error). Check this: https://tomcat.apache.org/tomcat-7.0-doc/servletapi/constant-values.html
使用request.getAttribute("javax.servlet.error.request_uri")获取未找到的请求页面的 URI(404 错误)。检查这个:https: //tomcat.apache.org/tomcat-7.0-doc/servletapi/constant-values.html
When error raised (because of some reason such as page not found (404), Internal Server Error (500), ...), the servlet engine will FORWARD the request to corresponding error page (configured in web.xml) using ERROR dispatcher type, NOT FORWARD dispatcher typeso that is the reason we must use javax.servlet.error.request_uri, NOT use javax.servlet.forward.request_uri
当出现错误时(由于某些原因,例如找不到页面(404)、内部服务器错误(500)等),servlet 引擎将使用ERROR 调度程序将请求转发到相应的错误页面(在 web.xml 中配置)type, NOT FORWARD 调度程序类型,所以这就是我们必须使用 javax.servlet.error.request_uri,而不是使用 javax.servlet.forward.request_uri 的原因
回答by Majid Ali Khan
You can do this to get the whole URL including parameters.
您可以执行此操作以获取包含参数的整个 URL。
request.getRequestURL()+""+request.getQueryString();