4

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.

Community
  • 1
  • 1
mjs
  • 21,431
  • 31
  • 118
  • 200
  • 1
    Use a servlet [`Filter`](https://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html) to catch exception and/or check response status code, and redirect as needed. – Andreas Nov 22 '15 at 09:53
  • @Andreas How would you do that? Set a filter mapped to `/*` and then? –  Apr 03 '17 at 19:17
  • 1
    @niklabaz Then write some code in the filter to "catch exception and/or check response status code, and redirect as needed". Which part of that in my original comment is unclear to you? How to "catch exception"? Use `try-catch` statement. How to "check response status code"? Use a `if` statement. How to "redirect"? Call [`forward()`](http://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html#forward-javax.servlet.ServletRequest-javax.servlet.ServletResponse-) to redirect response generation to error page of choice. – Andreas Apr 03 '17 at 21:16

1 Answers1

0

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

Tugrul ASLAN
  • 354
  • 1
  • 3
  • 16