tags:

views:

301

answers:

1

How can i get request URL from JSP.

If i use following code within JSP i get -

System.out.println("servlet path= " + request.getServletPath());
System.out.println("request URL= " + request.getRequestURL());
System.out.println("request URI= " + request.getRequestURI());

I get path to view(jsp) with jsp prefix. But i want to get URL from browser, that user type in address string. I can get valid URL in controller, that return jsp, then add it to view and get it within JSP. But prboably there are more elegant way to get valid URL within JSP ?

+3  A: 

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")

or

${requestScope['javax.servlet.forward.request_uri']}
axtavt
It's the other way round. He actually want the URL as it is shown in browser address bar.
BalusC
thank you, that was exactly what i was looking for
aauser
@aauser: in other words, you're actually **not** looking for the URL as it was shown in browser address bar but just for the URL of the forwarded view? (which doesn't appear in browser address bar at all). If so, then your question was pretty unclear and misleading.
BalusC
This is not url of forwarded view, this is a part of url in address bar. I can get url of forwarded view by request.getRequestURI()
aauser
@BalusC: He is completely right. When he forwards request to the view (using `RequestDispatcher.forward()`), request URI (returned by `getRequestURI()`) is replaced with the view URI. Original URI (as entered in the address bar) is saved as attribute.
axtavt
There are few examples here http://www.caucho.com/resin-3.0/webapp/faq.xtp
aauser
Yes, I now see what you mean.
BalusC