使用代码而不是 web.xml 以编程方式设置 <error-page>

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

我看过:

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

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

但是第一个是 tomcat 特有的,第二个没有很好的答案,而且它需要 Spring。

那么是否可以设置通常在 web.xml 中定义的错误页面,也许在 ServletListener 或类似的。

我的业余项目也遇到过类似的需求。我使用了具有状态代码和消息 fields.Eventually 的自定义未检查异常,这些字段将用于设置 HTTP 状态代码和消息本身。我在嵌入式 tomcat 9 上测试了该项目。检查我的存储库 https://github.com/tugrulaslan/BankingAPI