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.
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
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 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
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.