11

I'm redirecting 404 errors to a servlet via the following in my web.xml.

<error-page>
    <error-code>404</error-code>
    <location>/notFound.do</location>
</error-page>

I'd like to log where the request was trying to go, but I'm not getting it from the referrer header: request.getHeader("referer")

That shows 'null' if I just hit any old random non-existent page.

And request.getRequestURL()/request.getRequestURI() both merely shows the final landing servlet info (I.e., /notFound).

Any way to get the 'bad' page URL that was requested?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Eric Pierce
  • 187
  • 1
  • 9

1 Answers1

23

Yes, it's available as a request attribute with the name javax.servlet.forward.request_uri, which is keyed by RequestDispatcher#FORWARD_REQUEST_URI. The error page location is namely invoked by a simple RequestDispatcher#forward() call.

So, you can get it as follows in servlet:

String originalUri = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);

or in EL:

<p>Original URI: ${requestScope['javax.servlet.forward.request_uri']}</p>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It seems the servlet-api.jar (which provides javax.servlet.RequestDispatcher) in my Tomcat 7.0.19 install doesn't have any of those static fields; only the 'forward' and 'include' methods. But I can get it to work if I just put the string in directly. E.g., the following works. request.getAttribute("javax.servlet.forward.request_uri") Thanks for your help! – Eric Pierce Sep 21 '11 at 03:42
  • 1
    It's definitely present in there (click the links in my answer to see the javadocs). This problem suggests that you've configured your IDE to treat your project as a Servlet 2.4 project or older, or that you are relying your compiletime classpath on a loose and older versioned `servlet-api.jar` file which is placed elsewhere. Are you using Eclipse? If so, please read this: http://stackoverflow.com/questions/4076601/how-do-i-import-the-javax-servlet-api-in-my-eclipse-project/4076706#4076706 – BalusC Sep 21 '11 at 03:45
  • Weird. I popped open the servlet-api.jar and took a peek at the RequestDispatcher.class byte code, and I see FORWARD_REQUEST_URI in there (as well as the other constants). But opening the declaration within Eclipse only shows the two methods. They're both pointed to the same jar on the disk... – Eric Pierce Sep 21 '11 at 03:53
  • As said, that can happen when your project is configured as servlet 2.4 project. Eclipse will then hide all newer classes/methods. In project's properties, check in the *Project facets* section if the *Dynamic Web Project* facet is set to 2.5 or preferably 3.0 (Tomcat 7 is a Servlet 3.0 container). Also check if your `web.xml` is declared conform same servlet version. – BalusC Sep 21 '11 at 03:56
  • Ah... my web.xml was at 2.4. Thanks so much! BalusC, i'm looking for a way to contact you to get you something off your Amazon wish list. Can you IM me? eric555 @ jabber dot org – Eric Pierce Sep 21 '11 at 04:46