javatomcat

Prevent Tomcat from reporting 404 instead of a stack trace


Apparently, by default Tomcat will report certain errors in a servlet only on the first attempt. After that it will disable the WAR and return 404 on all subsequent attempts to access it.

I’m on Tomcat 10, and I have observed this on a bunch of occasions. As far as I can tell, all of these were conditions which would prevent the servlet from even starting (outdated servlet API version, class format too new for the server and so on).

That makes it a pain to debug. Is there a way to prevent Tomcat from disabling servlets on error, and return a 500 with a stack trace every time?


Solution

  • When Tomcat deploys a webapp but fails, this will be written to the logfile but the application is not deployed. If after that a client tries to access the application obviously that ends in a 404 result.Now the question is whether you are using lazy loading, which means some servlets or the whole application get deployed only when the first request comes in.

    Any way, you need to resolve these fatal issues as neither Tomat nor the webapp will be able to work without help. Check the logfile to find out the reason.

    One simple reason could be that the webapp requires some other resource that has not been deployed as e.g. a DB connection pool.

    And to come back to your question: I am not aware there is an option to turn this off. But you can either change your web.xml or use annotations to pre-load your servlets, which would give you the error messages not upon first requet but right at application deployment.

    Also read: