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