Search code examples
javajakarta-eecustom-error-pages

Programmatically set <error-page> using code rather than web.xml


I've seen:

How to set session-timeout, error-pages programmatically without web.xml

and

How to programmatically set error-page and etc web.xml parameters in spring

The first one is however specific to tomcat, and the second has not a good answer, plus it requires Spring.

So is it possible to set the error-page normally defined in web.xml, maybe in an ServletListener or similar.


Solution

  • I have come across a similar requirement for my side project. I used custom unchecked exceptions that have status code and message fields.Eventually those fields will be used to set the HTTP status code and the message itself. I tested the project on embedded tomcat 9. Check my repository https://github.com/tugrulaslan/BankingAPI