This week's book giveaway is in the Flex forum.
We're giving away four copies of Flex 4 in Action and have Tariq Ahmed, Dan Orlando, John C. Bland II & Joel Hooks on-line!
See this thread for details.
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Servlets
 
RSS feed
 
New topic
Author

How do you handle exceptions in init()

Ong Vua
Greenhorn

Joined: Jan 24, 2008
Messages: 21

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

Joined: Feb 23, 2009
Messages: 1060

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 -
Paul Clapham
Bartender

Joined: Oct 14, 2005
Messages: 7158

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
Greenhorn

Joined: Jan 24, 2008
Messages: 21

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 opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36508

Because a servlet is meant to service requests. A context listener is the correct place to init applications.

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Messages: 2304

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
Greenhorn

Joined: Jan 24, 2008
Messages: 21

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

Joined: Dec 11, 2009
Messages: 12

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

Joined: Mar 22, 2000
Messages: 10248

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

Java Resources at www.wbrogden.com
Bear Bibeault
Author and opinionated walrus
Sheriff

Joined: Jan 10, 2002
Messages: 36508

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.

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Servlets
 
RSS feed
 
New topic
The most intelligent Java IDE