在 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...
发布评论
评论(7)
仅当错误页面是由于异常而不是响应错误代码而加载时,才会设置
ErrorData
的exception
部分。请参阅 javadoc 了解
HttpServletResponse
上的 >sendError。 它提到了为什么您没有看到传递给sendError
的消息(强调我的):The
exception
part of theErrorData
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
onHttpServletResponse
. It mentions why you're not seeing the message you passed tosendError
(emphasis mine):你想要的东西对我来说看起来很奇怪:)。 也就是说,我会执行以下操作:
实现 HttpResponseWrapper 以这种方式包装任何其他
HttpResponse
:创建一个过滤器并在此包装任何响应
对所有请求进行过滤并在链中的第一个
在错误页面中检查响应是否为instanceof
HttpResponseWrapper
获取您的消息
The thing you want looks weird to me :). That said, I would do the following:
Implement
HttpResponseWrapper
to wrap any otherHttpResponse
in this way:Create a Filter and wrap any response in this
Put filter on all requests and first in the chain
In your error page check if response is instanceof
HttpResponseWrapper
Get your message
可以使用
如果页面没有jstl也
You can use
if you don't have jstl on the page
您可以使用
You can use
错误消息可通过错误页面 jsp 中请求对象的
javax.servlet.error.message
属性获得。以下是访问它的 jsp 语法:
您可以在错误页面中查找其他与错误相关的信息 此处位于新错误属性下。
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:
You could look for other error related information available in the error page here under New Error Attributes.
嗯
exception.getMessage()
应该可以尝试添加
exception.getClass().getName()
NullPointerException
当然,如果我没记错的话,只有当错误是由
<%@ page errorPage="/yourerrorpage.jsp" 的 jsp 抛出时,这才有效%>
在顶部。如果错误来自 servlet,则异常详细信息将作为请求属性传递
检查 Servlet 规范(链接自 2011 年起已损坏)第 9.9 节
Hmm
exception.getMessage()
should workTry adding
exception.getClass().getName()
NullPointerException
which has no messageOf 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
Check the Servlet Specification (link is broken since ~2011) section 9.9
我很抱歉这么晚才回答,但我一周前就遇到了这个问题,我浏览了很多不同的网站,但没有人真正按照我想听到的方式回答这个问题。 在这篇文章中,我发现了一些有趣的解决方案,然后提出了我自己的解决方案。 只需在您的页面中包含此来源即可:
它对我来说工作得很好。
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:
It worked perfectly fine with me.