This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan 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