Error page in web.xml
I have a such problem: i've set the dafault error page in web.xml on java.lang.Exception type, thus it has to be shown on all exceptions in servlets and jsp. But when I want to test this page(I turn off the connection) it doesn't apper in the browser. in tested servlet I use database, so if there is no connection it will throw an exception. In servlet I catch this exception and throw new ServletException()
. also in catch block at first I log the message and then throw an exception. So why my tomcat doesn't show this error page? Instead of this it sh开发者_如何转开发ow blank page and in server output I can see these error messages
edit
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorPages/InternalError.jsp</location>
</error-page>
i save error pages in /errorPages and the page that have to be shown called InternalError.jsp
This page has to be shown after moving to the jsp page which is using El to show data from database which in that jsp fall throw object that is in session
You will get a blank page in such situation when the response is already committed by the servlet or the JSP causing the exception. In other words, when the HTTP response headers are already been sent to the webbrowser. This is a point of no return.
To prevent this, you need to ensure that you start writing to the response only when all business code has finished its job (better, just don't do that in the servlet, but just forward to a JSP) and also that your JSPs does not contain any single line of business code (often represented by scriptlets <% %>
).
So if you do for example this in a servlet
response.getWriter().write("blah");
throw new ServletException("epic fail");
or when the exception is been thrown "halfway" a JSP
<p>Some HTML</p>
<% throw new ServletException("epic fail"); %>
<p>Some more HTML</p>
then the risk is big that you get a blank page.
精彩评论