6/09/2006

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:
java.lang.NullPointerException
test.HelloWorldServlet.doGet(HelloWorldServlet.java:14)
javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:73)
com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:120)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:231)
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.

22 comments:

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
sexy.co.uk

semi precious beads said...

Thanks for a wonderful share

Helen said...

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

Gawe Sare said...

This is one article that I find very interesting
many things that are really useful for me
maybe I will visit your blog again to find other unique information about

Toko Herbal QnC Jelly Gamat Obat Tiroiditis Cara Mengatasi Gatal Pada Puting Payudara QnC Jelly Gamat Cara Menghilangkan Jerawat Meradang Ahlinya Penyakit Kulit Obat Herbal Obat Gondok Beracun Penyebab Nyeri Dada Sebelah Kiri Cara Mengatasi Pengapuran Tulang dan Sendi

Kayal m said...

Blog was wrote with usefull information and very helpfull.keep sharing information with usMachine Learning Course in Tnagar
Machine Learning Traing in Tnagar
Machine Learning Course in Saidapet
Machine Learning Training in Nungambakkam
Machine Learning Training in Vadapalani
Machine Learning Training in Kodambakkam

T kaviranjan said...

http://javahowto.blogspot.com/2006/06/6-common-errors-in-setting-java-heap.html

kaviranjan said...

Attend the Best Machine learning training Courses in Bangalore From ExcelR. Practical Machine learningTraining Sessions with Assured Placement From Excelr Solutions.

machine learning course

priyanka said...

Attend The lean six sigma training in bangalore From ExcelR. Practical lean six sigma training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The lean six sigma training in bangalore.
lean six sigma training in bangalore

htop said...

thank you so much for this informations
best python training in chennai
best python training in sholinganallur
best python training institute in omr
python training in omr
selenium training in chennai
selenium training in omr
selenium training in sholinganallur

Priyanka said...

Attend The Python course in bangalore From ExcelR. Practical Python course in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python course in bangalore.
python course in bangalore

saketh said...


After reading your article I was amazed. I know that you explain it very well. And I hope that other Python training in pune readers will also experience how I feel after reading your article.

saketh said...


After reading your article I was amazed. I know that you explain it very well. And I hope that other Python training in pune readers will also experience how I feel after reading your article.

sorelle berkley crib reviews said...

Hi Guys. Please contact us if you got any quires or any confusions about Baby products. We would be honored if We can help you.

saketh said...


I just got to this amazing site not long ago. I was ExcelR Python classes in pune actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!

Parsan said...

Such an interesrting and essential topic that everyone should know this. for home PipeLine Leakage repair solution. Visit the site Pipeline Leak Detection to know more about Pipeline Leak Detection Service Provider in India.

mahi said...

Please refer below if you are looking for best project center in coimbatore

Java Training in Coimbatore | Digital Marketing Training in Coimbatore | SEO Training in Coimbatore | Tally Training in Coimbatore | Python Training In Coimbatore | Final Year IEEE Java Projects In Coimbatore | IEEE DOT NET PROJECTS IN COIMBATORE | Final Year IEEE Big Data Projects In Coimbatore | Final Year IEEE Python Projects In Coimbatore

Thank you for excellent article.

brninfotech said...

Thank you for your post. This is useful information.
Here we provide our special one's.
iphone app training course
iphone app development in hyderabad
mobile app training institutes
iphone apps training in hyderabad
iphone training institute in hyderabad

Dennis said...

Thanks for your blog!!.
JAVA Development Services
HR Pay Roll Software
SAP Software Services
Hotel Billing Software
Web Design Company
Hospital Management Software

mahi said...

Please refer below if you are looking for best project center in coimbatore

Hadoop Training in Coimbatore | CCNA Training in Coimbatore | AWS Training in Coimbatore | AngularJS Training in Coimbatore | Dotnet Training In Coimbatore | SAS Training In Coimbatore | R-Programming Training In Coimbatore

Thank you for excellent article.

Naveen said...

Please refer below if you are looking for best project center in coimbatore

Java Training in Coimbatore | Digital Marketing Training in Coimbatore | SEO Training in Coimbatore | Tally Training in Coimbatore | Python Training In Coimbatore | PMP Training In Coimbatore | Scrum Training In Coimbatore

Thank you for excellent article.