如何获取自定义错误页面(Tomcat)中的消息?

发布于 2024-07-23 11:46:15 字数 492 浏览 8 评论 0 原文

在 JSP 中,您可以使用 response.sendError(int code, String message) 返回特定的错误代码(例如,404 表示未找到)和消息。 只要您使用默认的丑陋的 Tomcat 错误页面,这些消息就可以正常显示。 但是,如果您创建自定义错误页面,如何获取该消息? 我尝试过 exception.getMessage()pageContext.getErrorData() 但无济于事。 我已经找了好几个小时了,似乎没有人对同样的事情感到好奇! :S

我忘了提及到目前为止我只尝试过 404,因为这是我最需要的...由于某种原因异常为 null,因此尝试任何内容都会抛出 NullPointerException。 错误页面是一个 404 错误页面,通过 web.xml 设置(因为我希望它为每个 404 错误显示),对于任何想知道的人来说,是的,它已将 isErrorPage 指令设置为 true...

In JSPs, you may use response.sendError(int code, String message) to return a particular error code (eg 404 for not found) and a message as well.
These messages display fine, as long as you use the default ugly Tomcat error pages. However, if you create a custom error page, how do you get that message?
I've tried exception.getMessage() or pageContext.getErrorData() but no avail. I've been searching for this for like hours and nobody seems to even wonder about the same thing! :S

I forgot to mention I've only tried it with 404s so far, since that's what I need most... The exception is null for some reason, so trying anything on it throws a NullPointerException.
The error page is a 404 error page, set via web.xml (since I want it to be displayed for EVERY single 404 error) and for anyone wondering, yes it has the isErrorPage directive set to true...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷情妓 2024-07-30 11:46:16

仅当错误页面是由于异常而不是响应错误代码而加载时,才会设置 ErrorDataexception 部分。

请参阅 javadoc 了解 HttpServletResponse 上的 >sendError。 它提到了为什么您没有看到传递给 sendError 的消息(强调我的):

使用指定的状态向客户端发送错误响应。 服务器默认创建的响应看起来像包含指定消息的 HTML 格式的服务器错误页面,将内容类型设置为“text/html”,不修改 cookie 和其他标头。 如果已针对与传入的状态代码相对应的 Web 应用程序进行了错误页面声明,则将优先于建议的 msg 参数返回该错误页面声明。

如果响应已提交,则此方法将抛出 IllegalStateException。 使用此方法后,响应应被视为已提交,而不应被写入。

The exception part of the ErrorData will only be set if the error page was loaded as a result of an exception and not a response error code.

See the javadoc for sendError on HttpServletResponse. It mentions why you're not seeing the message you passed to sendError (emphasis mine):

Sends an error response to the client using the specified status. The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html", leaving cookies and other headers unmodified. If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back in preference to the suggested msg parameter.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

掐死时间 2024-07-30 11:46:16

你想要的东西对我来说看起来很奇怪:)。 也就是说,我会执行以下操作:

  1. 实现 HttpResponseWrapper 以这种方式包装任何其他 HttpResponse

    公共类 HttpResponseWrapper 实现 HttpResponse { 
          私有字符串错误消息; 
      ... 
    
          @覆盖 
          公共无效发送错误(...){ 
              <在此处保存错误消息> 
          } 
      ... 
      } 
      
  2. 创建一个过滤器并在此包装任何响应

  3. 对所有请求进行过滤并在链中的第一个

  4. 在错误页面中检查响应是否为instanceof HttpResponseWrapper

  5. 获取您的消息

The thing you want looks weird to me :). That said, I would do the following:

  1. Implement HttpResponseWrapper to wrap any other HttpResponse in this way:

    public class HttpResponseWrapper implements HttpResponse {
        private String errorMessage;
    ...
    
        @Override
        public void sendError(...) {
            <save error message here>
        }
    ...
    }
    
  2. Create a Filter and wrap any response in this

  3. Put filter on all requests and first in the chain

  4. In your error page check if response is instanceof HttpResponseWrapper

  5. Get your message

初与友歌 2024-07-30 11:46:16

可以使用

${requestScope['javax.servlet.error.message']}

如果页面没有jstl也

You can use

${requestScope['javax.servlet.error.message']}

if you don't have jstl on the page

浅语花开 2024-07-30 11:46:16
<%
    if (null != request.getAttribute("errorMessage")) {
       out.println(request.getAttribute("errorMessage"));
    }
%>

您可以使用

<%
    if (null != request.getAttribute("errorMessage")) {
       out.println(request.getAttribute("errorMessage"));
    }
%>

You can use

最偏执的依靠 2024-07-30 11:46:15

错误消息可通过错误页面 jsp 中请求对象的 javax.servlet.error.message 属性获得。

以下是访问它的 jsp 语法:

<c:out value="${requestScope['javax.servlet.error.message']}"/>

您可以在错误页面中查找其他与错误相关的信息 此处位于新错误属性下。

The error message is available via javax.servlet.error.message attribute of the request object in error page jsp.

Here is the jsp syntax to access it:

<c:out value="${requestScope['javax.servlet.error.message']}"/>

You could look for other error related information available in the error page here under New Error Attributes.

最美的太阳 2024-07-30 11:46:15

exception.getMessage() 应该可以

尝试添加 exception.getClass().getName()

  1. 它可能是一个没有消息的 NullPointerException
  2. 或异常不是来自 Sun 并且消息设置不正确

当然,如果我没记错的话,只有当错误是由 <%@ page errorPage="/yourerrorpage.jsp" 的 jsp 抛出时,这才有效%> 在顶部。

如果错误来自 servlet,则异常详细信息将作为请求属性传递

javax.servlet.error.status_code    java.lang.Integer
javax.servlet.error.exception_type java.lang.Class
javax.servlet.error.message        java.lang.String
javax.servlet.error.exception      java.lang.Throwable
javax.servlet.error.request_uri    java.lang.String
javax.servlet.error.servlet_name   java.lang.String

检查 Servlet 规范(链接自 2011 年起已损坏)第 9.9 节

Hmm exception.getMessage() should work

Try adding exception.getClass().getName()

  1. It could be a NullPointerException which has no message
  2. or the exception is not from Sun and the message isn't set properly

Of course this only works, if I remember correctly, if the error is thrown by a jsp with <%@ page errorPage="/yourerrorpage.jsp" %> at the top.

If the error comes from a servlet the exception details are passed as request attributes

javax.servlet.error.status_code    java.lang.Integer
javax.servlet.error.exception_type java.lang.Class
javax.servlet.error.message        java.lang.String
javax.servlet.error.exception      java.lang.Throwable
javax.servlet.error.request_uri    java.lang.String
javax.servlet.error.servlet_name   java.lang.String

Check the Servlet Specification (link is broken since ~2011) section 9.9

时光与爱终年不遇 2024-07-30 11:46:15

我很抱歉这么晚才回答,但我一周前就遇到了这个问题,我浏览了很多不同的网站,但没有人真正按照我想听到的方式回答这个问题。 在这篇文章中,我发现了一些有趣的解决方案,然后提出了我自己的解决方案。 只需在您的页面中包含此来源即可:

<%
    out.println(pageContext.getErrorData().getRequestURI());
    out.println("<br/>");
    out.println(pageContext.getErrorData().getStatusCode());
    out.println("<br/>");
    out.println(pageContext.getException());
    out.println("<br/>");
%>

它对我来说工作得很好。

I'm sorry for answering so late, but I faced with this problem just a week ago, I've browsed a lot of different sites but nobody really aswered this problem the way I wanted to hear. In this post I found out a few interesting solutions and then came up to my own. Just include this source in your page:

<%
    out.println(pageContext.getErrorData().getRequestURI());
    out.println("<br/>");
    out.println(pageContext.getErrorData().getStatusCode());
    out.println("<br/>");
    out.println(pageContext.getException());
    out.println("<br/>");
%>

It worked perfectly fine with me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文