Sam Ingrassia
Sam Ingrassia

Reputation: 113

Working with errorPage directive with Servlet Filters in Java/JSP

Thanks to everyone in advance,

I have overridden HTTPServletResponse that collects headers and also put together a servlet filter that takes those headers and sets them using their proper methods (example: 500 error code uses response.sendError(500)). I am noticing that when this filter is set in the web.xml, any page that have the errorPage set is never used and the message i receive is the default tomcat 500 message without a stack trace The server encountered an internal error () that prevented it from fulfilling this request. Also nothing is written in the logs about what is happening. I have verified that the filter and the overridden HTTPServletResponse are not throwing any Exception. Another interesting note is that if I comment out the lines where I am using the sendError method it will use the errorPage specified, but then set the status code to 200.

Thanks again,

Sam

Upvotes: 1

Views: 2205

Answers (1)

cafebabe
cafebabe

Reputation: 1400

Well, the #sendError methods do not specifiy, that your custom error pages are to be used. It specifies, that the error condition is sent to the client immediately with flushing the response buffer at the end. Because the Servlet API is more general than the JSP API, the behavior is up to the container implementation. And the easiest way to send the error inside the Servlet layer is to show the container's default error page when using #sendError (that's what Tomcat does).

A solution is to chain the request / response so the servlet container's response processor can delegate to your custom error page instead of sending the error instantly.

Upvotes: 1

Related Questions