1

Lets say I hit

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.

I tried this code - <%= request.getAttribute("javax.servlet.forward.request_uri") %>; and it's returning me the path of 404.jsp

How can I get the entered URL which is there in address bar?

Zong
  • 6,160
  • 5
  • 32
  • 46
Vivek
  • 10,978
  • 14
  • 48
  • 66
  • possible duplicate of [Get full URL and query string in Servlet for both HTTP and HTTPS requests](http://stackoverflow.com/questions/16675191/get-full-url-and-query-string-in-servlet-for-both-http-and-https-requests) – earthmover May 22 '14 at 10:38
  • 1
    No, it's not duplicate of above question. – Vivek May 22 '14 at 10:42

5 Answers5

4

I think you were close. javax.servlet.forward.request_uri is for normal forwarding, but for 404, you need javax.servlet.error.request_uri.

mindas
  • 26,463
  • 15
  • 97
  • 154
4

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

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 type so that is the reason we must use javax.servlet.error.request_uri, NOT use javax.servlet.forward.request_uri

LHA
  • 9,398
  • 8
  • 46
  • 85
1

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()
earthmover
  • 4,395
  • 10
  • 43
  • 74
0

You can do this to get the whole URL including parameters.

    request.getRequestURL()+""+request.getQueryString();
Majid Ali Khan
  • 701
  • 8
  • 13
-2

use request.getHeader("Referer").

referer gives a url from where you redirected.

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77