Tell me please. How to make a global interception of errors occurring on the server side when ajax request for a servlet. By global, I mean one mechanism, be it a class or a function that intercepts Exception arising in .class files before an answer. Now it works in such a way that I get all the error information in response - this is the status (code - 500, 400, etc.), the message (for example, java.lang.NumberFormatException: null ) and the so-called StackTrace (error details - indication of classes, etc.). I only need to reply with the status and message and display it to the user. How to do it?

I use Servlet API

  • Add details to the question. JavaEE is a fairly broad concept. What exactly are you using: Spring, JAX-RS, pure Servlet API? - Nofate
  • @Nofate Servlet API - Tsyklop pm

1 answer 1

register in web.xml

  <web-app ...> <error-page> <location>/errorPage</location> </error-page> </web-app> 

or

  <web-app ...> <error-page> <error-code>500</error-code> <location>/errorPage</location> </error-page> </web-app> 
  • Will this work for ajax requests? - Tsyklop
  • Will work for all requests to the application - MrFylypenko
  • AJAX is a word that means nothing to a server. The differences are only on the client side: in that these are asynchronous requests. - Janislav Kornev