java - Tomcat container fails to use <error-page> configuration if exception occurs after the response object's OutputStream is opened -
i'm using tomcat 5.5 , spring 3.2. in web.xml deployment descriptor, have following configuration -
<error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/403.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
the unexplained behavior occurs in method similar 1 below -
public modelandview filedownload(httpservletrequest request, httpservletresponse response) throws exception { string filename = (string) request.getparameter("filename"); //...more code file file = new file(...+ filename); // pseudo code outputstream out = response.getoutputstream(); inputstream stream = new fileinputstream(file); // filenotfoundexception here //...more code }
when filenotfoundexception occurs, don't see custom 500.jsp used render error page, instead see entire stack trace of exception on page. if reverse order of 2 statements below,
inputstream stream = new fileinputstream(file); // filenotfoundexception here outputstream out = response.getoutputstream();
everything works correctly , correct 500.jsp rendered on page.
why occur? difference in latter case, outputstream of response object not opened.
under hood, jsp attempting call getwriter method on httpservletresponse. according spec, getwriter can called or getoutputstream can called, not both. since have called getoutputstream, jsp unable write response.
http://docs.oracle.com/javaee/5/api/javax/servlet/servletresponse.html#getoutputstream()
http://comments.gmane.org/gmane.comp.jakarta.tomcat.user/182025
Comments
Post a Comment