I'd like to handle HTML 404 errors in my web app.
I can write this:
<error-page>
<error-code>404</error-code>
<location>/view/error404.jsp</location>
</error-page>
This works perfectly, but I want to log each of the invalid urls that the users type. When I use a scriptlet in the error404.jsp like this:
<%
System.out.println(request.getRequestURL());
%>
I always get : http://localhost:8080/webApp/view/error.jsp, because the users will be forwarded to my error404.jsp from the invalid URL.
How should I get the invalid URL? Or how should I write a servlet that catch all requests that are not handled explicitly by other servlets?
It's stored as request attribute with the key javax.servlet.forward.request_uri
:
<p>URL: ${requestScope['javax.servlet.forward.request_uri']}</p>
Or if you're still on the legacy JSP 1.x which was already been upgraded over a decade ago, then do so:
<p>URL: <%= request.getAttribute("javax.servlet.forward.request_uri"); %></p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With