获取Servlet转发的JSP中的请求URL [英] Get request URL in JSP which is forwarded by Servlet

查看:127
本文介绍了获取Servlet转发的JSP中的请求URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Servlet转发的JSP中获取请求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']}

这篇关于获取Servlet转发的JSP中的请求URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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