Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP / Servlet HTTP 404 error handling

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?

like image 495
Pink Avatar asked Jan 21 '23 16:01

Pink


1 Answers

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>
like image 164
BalusC Avatar answered Feb 01 '23 22:02

BalusC