A Common Mistake in Servlet init Methods

Servlet init methods allows a servlet to perform one-time initialization prior to servicing requests. One common mistake when implementing init method is in this form:

public void init(ServletConfig config)
throws ServletException {
//do custom initialization ...
System.out.println(" init(ServletConfig config) invoked.");
This is wrong because it doesn't invoke super.init(ServletConfig). As a result, ServletConfig is not stored in the servlet instance, and subsequent calls to getServletConfig will return null.

Another variant of this mistake is to store the ServletConfig parameter in a class variable of the concrete servlet class:
private ServletConfig config;

public void init(ServletConfig config)
throws ServletException {
this.config = config;
System.out.println(" init(ServletConfig config) invoked.");
//do custom initialization ...
This is also wrong because config saved in the current servlet won't be available to its superclass, usually GenericServlet or HttpServlet. When the default implementation of getServletConfig() method looks for ServletConfig in superclasses, it's still null. The only way it can work is that you also override getServletConfig method to look in the concrete servlet class, which is unusual and unnecessary.

This is the error from Glassfish/SJSAS 9.0/JavaEE SDK 5, when running a servlet with those incorrect init methods:
To implement servlet init methods correctly, you have several options:
  • If you only need to save ServletConfig, do not override any init methods in servlet class. It's already implemented in servlet superclass GenericServlet.

  • If you have custom initialization work to do, override the no-arg init() method, and forget about init(ServletConfig). Is it ok to call getServletConfig() method inside the no-arg init() method? Yes, an instance of ServletConfig has already been saved by superclass GenericServlet. See Why Servlet init Methods are Confusing for more details.

  • If you really want to override init(ServletConfig), make sure you invoke super.init(ServletConfig);, usually as the first line, though it's not strictly required.


Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Denisa Cretu said...

It is the best time to make plans for the future and it's time to be happy. I have read this post and if I could I wish to suggest you few interesting things or advice. Maybe you could write next articles referring to this article. I want to read even more things about it. Great blog. Thanks!
juegosfrival , juegoskizi20 , friv30game

Ariel said...

Thank you for posting such topics

semi precious beads said...

Thanks for a wonderful share

Helen said...

You describe it well. I'm glad you wrote this in the forum.