Vivek
Vivek

Reputation: 11028

how to get URL from address bar in JSP

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?

Upvotes: 1

Views: 18140

Answers (5)

Majid Ali Khan
Majid Ali Khan

Reputation: 741

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

    request.getRequestURL()+""+request.getQueryString();

Upvotes: 0

LHA
LHA

Reputation: 9655

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

Upvotes: 4

mindas
mindas

Reputation: 26713

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

Upvotes: 4

earthmover
earthmover

Reputation: 4525

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

Upvotes: 1

Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

use request.getHeader("Referer").

referer gives a url from where you redirected.

Upvotes: -2

Related Questions