Error page in web.xml

I have this problem: I set the dafault error page in the web.xml file like java.lang.Exception, so it should be displayed in all exceptions in servlets and jsp. But when I want to test this page (I will disconnect the connection), it does not work in the browser. in a tested servlet I use a database, so if there is no connection, it will throw an exception. In the servlet I will catch this exception and throw new ServletException(). also in the catch block, I first log the message and then throw an exception. So why is my tomcat not showing this error page? Instead, it shows a blank page and on the server output I can see these error messages

change

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

i save the error pages to / errorPages and the page to be displayed with the name InternalError.jsp

This page should be displayed after going to the jsp page that uses El to display data from the database, which in this jsp fall throw an object that is in the session

+3
source share
1 answer

In this situation, you will get a blank page when the response has already been sent by the servlet or JSP that throws the exception. In other words, when the HTTP response headers are already sent to the web browser. This is a point with no return .

, , , - (, , JSP), JSP - ( <% %>).

, , ,

response.getWriter().write("blah");
throw new ServletException("epic fail");

"" JSP

<p>Some HTML</p>
<% throw new ServletException("epic fail"); %>
<p>Some more HTML</p>

, .

+2

All Articles