This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi, friends. I understand that if a servlet has an init() method, then the first line of the method should be a call to super.init(config). My question is, though, when should a servlet have an init() method at all, and when should it not? In some cases, it is absolutely clear that you need an init() method: for example, when your design dictates that you open your database connection one time only, when the servlet loads. But my question is more, what is the factor that decides for you that you should include only these three lines:
You should only override the init() method when you need to do some servlet configuration that must be done before the servlet is running, such as setting up a DB connection pool. Otherwise just let it do its thing.
I relation to this subject, Marty Hall in his "Core Servlets and JavaServer Pages" book, page 36, states that: "If you write an init method that takes a ServletConfig as an argument, always call super.init on the first line."
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Joined: Oct 31, 2000
Frank, Chris and Tony-- Thank you for clearing this up for me! Art