JSP: How can I get the code on my error page, even if I can’t display it?

I defined the error page in my web.xml:

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

On this error page, I have my own tag that I created. The tag handler for this tag emails me a stack of any error. For the most part this works great.

In cases where it does not work perfectly, if the output has already been sent to the client at the time the error occurred. In this case, we get the following:

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

I believe this error occurs because we cannot redirect the request to the error page after the exit is already running. The work I used was to increase the buffer size on especially 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 if you can go through hundreds of JSP pages, making sure their buffers are big enough.

Is there a way to allow execution of my stack trace mail code in this case, even if I cannot display the error page to the client?

+3
source share
3 answers

ErrorPage , . JavaScript . include -, boolean 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>

jsp true:

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

<% @page errorPage = "/myerrorpage.jsp" % > ?

<% page isErrorPage = "true" $ > myerrorpage.jsp.

, . , JSP.

+1

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

+1
source

Source: https://habr.com/ru/post/1698377/


All Articles