Get the URL of the requested page that called 404

How do I get the URL of the requested page that caused the 404 error?

For example, I type, I go to http://example.com/path/does/not/exist/index.jsp I already have a 404 user page, but how can I find the URL mentioned above so that I can display it with a message similar to "URL http://example.com/path/does/not/exist/index.jsp does not exist ??

+6
jsp
source share
2 answers

If you used a forward to go to the error page, you can get the source URL of the request using

request.getAttribute("javax.servlet.forward.request_uri") 

or EL

 ${requestScope['javax.servlet.forward.request_uri']} 
+9
source share

I am using JDK 8 and GlassFish 4, and

 request.getAttribute("javax.servlet.forward.request_uri") 

always returned null for me. Ultimately the work was

 request.getAttribute("javax.servlet.error.request_uri") 

I'm not sure why the attribute name is different, but if the name I provided does not work in your setup, here is the code I used to find it ...

  Enumeration<String> names = request.getAttributeNames(); while(names.hasMoreElements()){ String name = names.nextElement(); Object attr = request.getAttribute(name); System.out.println("Req: "+name + " : "+attr); } 
0
source share

All Articles