Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to catch Servlet.init() exceptions in spring  RSS feed

 
preetika shukla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi team,
I am a bit new to Spring framework. In my application,we are using servlet-context.xml to create the beans required for  DB. For some reason say at some time if the DB was down, or some other bean creation erred out, I am getting a Servlet.init() error and the complete stack trace is being shown at the jsp context.
Is it possible to catch these exceptions and throw a minimalist error saying, something went wrong, so that the end user isn't being shown all the stack trace.
I have tried using the controllerAdvice annotation as suggested by everyone, But if the sping application was itself unable to bootstrap, how will these annotations help me out?      
stuck on this since two dats 
Any insights will be deeply appreciated.
Regards,
jeangrey
 
Ankit Garg
Sheriff
Posts: 9608
36
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preetika, welcome to CodeRanch.

I think in such a case, you'll have to define the error page in web.xml as I believe the stack trace is shown by the default error page of Tomcat (or whatever server you are using). See examples here on how to setup your own error page in web.xml...
 
preetika shukla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Hi Preetika, welcome to CodeRanch.

I think in such a case, you'll have to define the error page in web.xml as I believe the stack trace is shown by the default error page of Tomcat (or whatever server you are using). See examples here on how to setup your own error page in web.xml...


Thanks Ankit  , But i see the references you've provided are for standard J2ee , where servlets are serving the requests using doget/doPost methods. is it possible to redo the same using spring controllers ?

 
Ankit Garg
Sheriff
Posts: 9608
36
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring also works by adding a Servlet to your web.xml (DispatcherServlet) so I would assume it would work as your DispatcherServlet is probably using the default error page of the server when the Spring startup fails. Did you try it and it didn't work? Since you are talking about a scenario where the context start fails, I don't know how you'll be able to do it with a Spring controller...
 
preetika shukla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Spring also works by adding a Servlet to your web.xml (DispatcherServlet) so I would assume it would work as your DispatcherServlet is probably using the default error page of the server when the Spring startup fails. Did you try it and it didn't work? Since you are talking about a scenario where the context start fails, I don't know how you'll be able to do it with a Spring controller...


Ankit, thanks for the help, I've used the error-page in web.xml   
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
preetika shukla wrote:hi team,
I am a bit new to Spring framework. In my application,we are using servlet-context.xml to create the beans required for  DB. For some reason say at some time if the DB was down, or some other bean creation erred out, I am getting a Servlet.init() error and the complete stack trace is being shown at the jsp context.
Is it possible to catch these exceptions and throw a minimalist error saying, something went wrong, so that the end user isn't being shown all the stack trace.
I have tried using the controllerAdvice annotation as suggested by everyone, But if the sping application was itself unable to bootstrap, how will these annotations help me out?      
stuck on this since two dats 
Any insights will be deeply appreciated.
Regards,
jeangrey


Hi, Preetika, my two cents is to avoid exposing Servlets as a technology behind your Spring-based application as much as possible except for addons that depend on it directly, such as Spring Security.

With best regards,

Anton.

 
preetika shukla
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, Preetika, my two cents is to avoid exposing Servlets as a technology behind your Spring-based application as much as possible except for addons that depend on it directly, such as Spring Security.

With best regards,

Anton.



Anton,
Thanks for the insights but are you aware of any other way of doing this ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!