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
I'm looking for the best way to handle exceptions in the init() or doGet(), doPost().
Thanks
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1169
posted
0
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.
JDBCSupport - An easy to use, light-weight JDBC framework -
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
Joined: Jan 24, 2008
Posts: 38
posted
0
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?
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
Joined: Jan 24, 2008
Posts: 38
posted
0
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.
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
Joined: Apr 23, 2006
Posts: 2
posted
0
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..