【问题标题】:Servlet: custom 401 page isn't triggeredServlet:未触发自定义 401 页面
【发布时间】:2012-03-09 20:26:35
【问题描述】:

我注册了一个错误页面:

<error-page>
    <error-code>401</error-code>
    <location>/WEB-INF/exceptions/401.jsp</location>
</error-page>

我在我的 Servlet 中触发了一个错误:

response.sendError(401, "message here");

401.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
    ...
    <body>
        <h1>Unauthorized (401)</h1>
        <p><%= exception.getMessage() %></p>
    </body>    
</html>

很遗憾,仅显示“通用”401 页面。

我已经重新启动了应用程序和 Tomcat,但没有任何变化。

我错过了什么?

** 编辑 0 **

我包含了 JSP 文件的代码。

看来问题出在&lt;%= exception.getMessage() %&gt; 代码上。如果将其删除,自定义页面将按预期显示。如果存在,则会显示“通用”消息。

我替换了这个 JSTL ${pageContext.errorData.throwable.message}。显示自定义 401 页面,但消息为空白。

【问题讨论】:

标签: tomcat servlets


【解决方案1】:

AFAIK jsp 应该在 WEB-INF 文件夹之外,否则你必须在 web.xml 中使用 &lt;jsp-file&gt; 标签配置你的 jsp。尝试将401.jsp放在WEB-INF之外。

【讨论】:

  • 将 JSP 放在 WEB-INF 中是一种很好的做法,因为它可以防止未经授权的访问。问题在于嵌入的代码,而不是它的位置。
猜你喜欢
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
相关资源
最近更新 更多