JSP / Servlet HTTP 404 Error Handling

I would like to handle HTML 404 errors in my web application.

I can write this:

<error-page> <error-code>404</error-code> <location>/view/error404.jsp</location> </error-page> 

This works fine, but I want to log every invalid URL that the user enters. When I use the scriptlet in the error404.jsp file as follows:

  <% System.out.println(request.getRequestURL()); %> 

I always get: http: // localhost: 8080 / webApp / view / error.jsp , because users will be redirected to my error404.jsp from an invalid URL.

How to get an invalid URL? Or how should I write a servlet that catches all requests that are not explicitly processed by other servlets?

+4
source share
1 answer

It is stored as a request attribute with the javax.servlet.forward.request_uri key:

 <p>URL: ${requestScope['javax.servlet.forward.request_uri']}</p> 

Or, if you are still on the obsolete JSP 1.x, which was already updated more than ten years ago, do the following:

 <p>URL: <%= request.getAttribute("javax.servlet.forward.request_uri"); %></p> 
+9
source

All Articles