JSP:即使我无法显示,我怎样才能在错误页面上运行代码?

时间:2022-04-16 02:59:52

I've defined an error-page in my web.xml:

我在web.xml中定义了一个错误页面:

 <error-page>
   <exception-type>java.lang.Exception</exception-type>
   <location>/error.jsp</location>
 </error-page>

In that error page, I have a custom tag that I created. The tag handler for this tag e-mails me the stacktrace of whatever error occurred. For the most part this works great.

在该错误页面中,我有一个我创建的自定义标记。此标记的标记处理程序通过电子邮件发送任何错误的堆栈跟踪。在大多数情况下,这很有效。

Where it doesn't work great is if the output has already begun being sent to the client at the time the error occurs. In that case, we get this:

如果输出在错误发生时已经开始发送到客户端,那么它不能正常工作。在这种情况下,我们得到这个:

SEVERE: Exception Processing ErrorPage[exceptionType=java.lang.Exception, location=/error.jsp]
 java.lang.IllegalStateException

I believe this error happens because we can't redirect a request to the error page after output has already started. The work-around I've used is to increase the buffer size on particularly large JSP pages. But I'm trying to write a generic error handler that I can apply to existing applications, and I'm not sure it's feasible to go through hundreds of JSP pages making sure their buffers are big enough.

我相信发生此错误是因为我们无法在输出已经开始后将请求重定向到错误页面。我使用的解决方法是在特别大的JSP页面上增加缓冲区大小。但是我正在尝试编写一个可以应用于现有应用程序的通用错误处理程序,而且我不确定通过数百个JSP页面来确保它们的缓冲区足够大是可行的。

Is there a way to still allow my stack trace e-mail code to execute in this case, even if I can't actually display the error page to the client?

有没有办法在这种情况下仍允许我的堆栈跟踪电子邮件代码执行,即使我实际上无法将错误页面显示给客户端?

3 个解决方案

#1


2  

The errorPage isn't going to be used if you've already started sending data to the client. What I do is use a JavaScript callback to check for an incomplete page and then redirect to the error page. At the beginning of your page in an includes header or something, initialize a boolean javascript variable to false, and register an onload handler to check the state and redirect to an error page.

如果您已经开始向客户端发送数据,则不会使用errorPage。我所做的是使用JavaScript回调来检查不完整的页面,然后重定向到错误页面。在包含标题或页面的页面的开头,将布尔javascript变量初始化为false,并注册onload处理程序以检查状态并重定向到错误页面。

<script type="text/javascript">
        var pageLoadSuccessful = false;//set to true in footer.jsp
        dojo.addOnLoad(function(){
            if (!pageLoadSuccessful) window.location = "<c:url value="/error.do" />";
        });
</script>

Then in a footer jsp, be sure to set this variable to true:

然后在页脚jsp中,确保将此变量设置为true:

<script type="text/javascript">
    pageLoadSuccessful = true;//declared in header.jsp
</script>

#2


1  

Have you tried using the <%@ page errorPage="/myerrorpage.jsp" %> directive?

您是否尝试过使用<%@ page errorPage =“/ myerrorpage.jsp”%>指令?

You also need to use <% page isErrorPage="true" $> in myerrorpage.jsp, then.

您还需要在myerrorpage.jsp中使用<%page isErrorPage =“true”$>,然后。

I think that may solve your problem. The only problem with that is that you need to include it in every JSP somehow.

我认为这可以解决你的问题。唯一的问题是你需要以某种方式将它包含在每个JSP中。

#3


1  

In fact, this particular problem indicates that you were using scriptlets in JSP. This is a bad practice and this particular problem is one of the major reasons for that. You need to move all your business logic to a real java class so that you end up with only taglibs/EL in JSP. In a servlet/filter you can perfectly handle exceptions before forwarding the request to a JSP.

实际上,这个特殊问题表明您在JSP中使用了scriptlet。这是一种不好的做法,这个特殊问题是其中一个主要原因。您需要将所有业务逻辑移动到真正的Java类,以便最终只在JSP中使用taglibs / EL。在servlet /过滤器中,您可以在将请求转发到JSP之前完美地处理异常。

#1


2  

The errorPage isn't going to be used if you've already started sending data to the client. What I do is use a JavaScript callback to check for an incomplete page and then redirect to the error page. At the beginning of your page in an includes header or something, initialize a boolean javascript variable to false, and register an onload handler to check the state and redirect to an error page.

如果您已经开始向客户端发送数据,则不会使用errorPage。我所做的是使用JavaScript回调来检查不完整的页面,然后重定向到错误页面。在包含标题或页面的页面的开头,将布尔javascript变量初始化为false,并注册onload处理程序以检查状态并重定向到错误页面。

<script type="text/javascript">
        var pageLoadSuccessful = false;//set to true in footer.jsp
        dojo.addOnLoad(function(){
            if (!pageLoadSuccessful) window.location = "<c:url value="/error.do" />";
        });
</script>

Then in a footer jsp, be sure to set this variable to true:

然后在页脚jsp中,确保将此变量设置为true:

<script type="text/javascript">
    pageLoadSuccessful = true;//declared in header.jsp
</script>

#2


1  

Have you tried using the <%@ page errorPage="/myerrorpage.jsp" %> directive?

您是否尝试过使用<%@ page errorPage =“/ myerrorpage.jsp”%>指令?

You also need to use <% page isErrorPage="true" $> in myerrorpage.jsp, then.

您还需要在myerrorpage.jsp中使用<%page isErrorPage =“true”$>,然后。

I think that may solve your problem. The only problem with that is that you need to include it in every JSP somehow.

我认为这可以解决你的问题。唯一的问题是你需要以某种方式将它包含在每个JSP中。

#3


1  

In fact, this particular problem indicates that you were using scriptlets in JSP. This is a bad practice and this particular problem is one of the major reasons for that. You need to move all your business logic to a real java class so that you end up with only taglibs/EL in JSP. In a servlet/filter you can perfectly handle exceptions before forwarding the request to a JSP.

实际上,这个特殊问题表明您在JSP中使用了scriptlet。这是一种不好的做法,这个特殊问题是其中一个主要原因。您需要将所有业务逻辑移动到真正的Java类,以便最终只在JSP中使用taglibs / EL。在servlet /过滤器中,您可以在将请求转发到JSP之前完美地处理异常。