获取JSP中由Servlet转发的请求URL [英] Get request URL in JSP which is forwarded by Servlet
问题描述
如何在 JSP 中获取 Servlet 转发的请求 URL?
How can I get request URL in JSP which is forwarded by Servlet?
如果我在 JSP 中运行以下代码,
If I run following code in JSP,
System.out.println("servlet path= " + request.getServletPath());
System.out.println("request URL= " + request.getRequestURL());
System.out.println("request URI= " + request.getRequestURI());
然后我得到 JSP 的服务器端路径.但我想获取您在浏览器地址栏中看到的 URL.我可以在转发到 JSP 的 Servlet 中获取它,但我想在 JSP 中获取它.
then I get the server side path to the JSP. But I want to get the URL as you can see in browser's address bar. I can get it in the Servlet that forwards to the JSP, but I want to get it within the JSP.
推荐答案
如果使用 RequestDispatcher.forward()
将请求从控制器路由到视图,则请求 URI 将作为请求公开名为 javax.servlet.forward.request_uri
的属性.所以,你可以使用
If you use RequestDispatcher.forward()
to route the request from controller to the view, then request URI is exposed as a request attribute named javax.servlet.forward.request_uri
. So, you can use
request.getAttribute("javax.servlet.forward.request_uri")
或
${requestScope['javax.servlet.forward.request_uri']}
这篇关于获取JSP中由Servlet转发的请求URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!