【问题标题】:JSP / Servlet HTTP 404 error handlingJSP/Servlet HTTP 404 错误处理
【发布时间】:2011-03-21 04:09:23
【问题描述】:

我想在我的网络应用中处理 HTML 404 错误。

我可以这样写:

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

这很好用,但我想记录用户键入的每个无效 url。 当我在 error404.jsp 中使用 scriptlet 时:

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

我总是得到:http://localhost:8080/webApp/view/error.jsp,因为用户将从无效的 URL 转发到我的 error404.jsp。

我应该如何获取无效的 URL?或者我应该如何编写一个 servlet 来捕获所有未被其他 servlet 显式处理的请求?

【问题讨论】:

    标签: html jsp http-status-code-404


    【解决方案1】:

    它被存储为请求属性,键为javax.servlet.forward.request_uri

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

    或者,如果您仍在使用十多年前已经升级的旧版 JSP 1.x,那么请这样做:

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

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 2015-04-07
      • 2014-02-01
      • 2012-11-16
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      相关资源
      最近更新 更多