JSP / Servlet HTTP 404 error handling

不打扰是莪最后的温柔 提交于 2019-12-10 16:12:01

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/3336501/jsp-servlet-http-404-error-handling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!