3

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 show 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

maks
  • 5,911
  • 17
  • 79
  • 123

1 Answers1

2

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • By doing the business job in a servlet instead of a JSP. The JSP should be used for presentation only. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC May 25 '11 at 19:52
  • I don't have business logic in jsp. Jsp only contains the formatted output from object that have access to database like that: where Obj is the class that has method getInfo which is accessing to database – maks May 25 '11 at 20:01
  • You should not be doing any business logic in a getter method. Do it in the constructor instead, or if it really needs to be executed on a per-request basis, then you should be putting it in the request scope. This way the exception will be thrown when the servlet is about to create it and store it in the scope, long before the request is been forwarded to the JSP. – BalusC May 25 '11 at 20:02
  • if I will load all data in constructor when the oblect of Obj class is created it will use a large amount of memory for all the time my program is running. I have many such get methods which access to db on demand – maks May 25 '11 at 20:05
  • Is it such an `God` class? Then the code definitely needs refactoring. You need to end up doing the business job in the servlet (controller) instead of the JSP (view) as per the MVC ideology. – BalusC May 25 '11 at 20:07
  • When I directly specify the error page in <%@ errorPage="InternalError.jsp"%> in that jsp, it works. But I have a lot of jsp and maybe it will be much more. – maks May 25 '11 at 20:19
  • This is actually worse. You need to pray that the response isn't committed at that point. Most servletcontainers defaults to flushing the response every 2KB of data sent. – BalusC May 25 '11 at 20:21
  • So, if i have in one page a link to showAllObj.jsp I have to replace it by servlet like showAllObjServlet in which I will get the data from db and then redirect to showAllObj.jsp where simply output the data? – maks May 25 '11 at 20:32
  • Almost good, you need to forward it, not redirect it. With redirecting all request attributes get lost because a redirect basically instructs the browser to create a new request. – BalusC May 25 '11 at 20:34
  • I get to know that if i use a filter than any exception that will be occured will be catched in the filter, so to get the user an errorPage in this case i have to throw the exception in filter. In my project all jsps that can throw an exception are under the filter – maks May 25 '11 at 22:40
  • You might still want to increase the container's default response buffer size. – BalusC May 25 '11 at 22:45