Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you handle exceptions in init()  RSS feed

 
Ong Vua
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my init(ServletConfig config) method, I have exceptions being thrown. How do you usually handle it? Do you recommend creating a new ServletException with some description of why the exception is being thrown with the original exception as below or just do a printstacktrace

try {

catch (SchedulerException e) {
ServletException se = new ServletException("Scheduler can't be created", e);
throw ServletException;
}

I'm looking for the best way to handle exceptions in the init() or doGet(), doPost().

Thanks
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on how critical the exception is to your servlet cycle.

If the exception means that something essential went wrong and the rest of the servlet cannot run properly then you better propagate it and throw a wrappered ServletException. If it is just a minor error you can simply print a stacktrace as a notifier.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:If the exception means that something essential went wrong and the rest of the servlet cannot run properly then you better propagate it and throw a wrappered ServletException.


And, turning that around: if you throw an exception then the servlet will be unusable. Any requests to it will cause the container to reply with a response code in the 500 series (don't remember which).

By the way, initializing an application-wide resource is better done in a ServletContextListener, rather than in one of the servlets in the application. Although that doesn't make the question of what to do about exceptions go away.
 
Ong Vua
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? So I should not do my initializing an application-wide resource in init()? Can I know why?

Right now, I have a Servlet that extends from HttpServlet. I should implement ServletContextListener and do my initialization in the method below?

public void contextInitialized(ServletContextEvent contextEvent) {}

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because a servlet is meant to service requests. A context listener is the correct place to init applications.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ong Vua wrote:Right now, I have a Servlet that extends from HttpServlet. I should implement ServletContextListener and do my initialization in the method below?


You mean your servlet is going to implement ServletContextListener?if yes, dont do this. Create Seperate Listener class
 
Ong Vua
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by creating a separate listener class? Can you elaborate more and how do I call it from my servlet. Right now, my servlet will start automatically when WL started and I want to initialize application resources when it's started up.
 
Pavan Kumar Reddy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
visit http://servlets-jsp.blogspot.com to know about how to write exception in init(ServletConfig) method
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see no reason to excessively complicate life for beginning servlet programmers with
A context listener is the correct place to init applications.

doctrine. Initializing in init() works just fine. Handle exceptions by logging the cause and throwing UnavailableException if necessary.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... I'll have to respectfully disagree. Using a servlet's init() as a side-effect to initialize an application is very old-fashioned and just a wrong use of a tool. I see nothing confusing about listeners. They are a concept that even beginners need to know about, and novices should learn how to do things properly from the beginning.
 
Shashank Jain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Sorry... I'll have to respectfully disagree. Using a servlet's init() as a side-effect to initialize an application is very old-fashioned and just a wrong use of a tool. I see nothing confusing about listeners. They are a concept that even beginners need to know about, and novices should learn how to do things properly from the beginning.


I agree that we should do initialization in a Listener..In case the initialization code is doing something like looking for a resource or getting some web application configuration information which might not be ready and may cause the init to fail . Constructor is too early a place to do initialization. It has to be Listener doing the initializations..

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!