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.

74 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

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

iuwekjhbr said...

Please refer below if you are looking for best Online job support and proxy interview from India

DevOps Online Job Support From India | PHP Online Job Support From India | Selenium Online Job Support From India | Hadoop Online Job Support From India | Java Online Job Support From India | Angular Online Job Support From India | Python Online Job Support From India | Android Online Job Support From India

Thank you for excellent article.

Unknown said...

Please refer below if you are looking for best Online job support and proxy interview from India

AWS Online Job Support From India | Workday Online Job Support From India | ReactJS Online Job Support From India | Manual Testing Online Job Support From India | Dotnet Online Job Support From India | Peoplesoft Online Job Support From India | Teradata Online Job Support From India

Thank you for excellent article.

iuwekjhbr said...

Please refer below if you are looking for best Online job support and proxy interview from India

DevOps Proxy Interview Support From India | PHP Proxy Interview Support From India | Selenium Proxy Interview Support From India | Hadoop Proxy Interview Support From India | Java Proxy Interview Support From India | Angular Proxy Interview Support From India | Python Proxy Interview Support From India | Android Proxy Interview Support From India

Thank you for excellent article.

mahi said...

Please refer below if you are looking for best Training 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.

Naveen said...

Please refer below if you are looking for best Training 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.

dataexpert said...


I see the greatest contents on your blog and I extremely love reading ExcelR Data Scientist Classes In Pune

Advanced Diploma in Embedded Systems said...

thanks for sharing this nice infoamtion..i really enjoyed to read your information.learn bench india is the best project center in chennai..
to know more about this best project center in chennai
best final year project center in chennai
best final year ieee project center in chennai
best embedded project center in chennai

bavisthra said...

Study Machine Learning Training in Bangalore with ExcelR where you get a great experience and better knowledge .
Machine Learning Training in Bangalore

KARTHIK said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
http://trainingsinvelachery.in/sap-hr-training-in-velachery/
http://trainingsinvelachery.in/sap-mm-training-in-velachery/
http://trainingsinvelachery.in/sap-sd-training-in-velachery/
http://trainingsinvelachery.in/sap-fico-training-in-velachery/
http://trainingsinvelachery.in/sap-abap-training-in-velachery/
http://trainingsinvelachery.in/sap-hana-training-in-velachery/

Data Science Course said...

Really awesome blog!!! I finally found a great post here.I really enjoyed reading this article. Thanks for sharing valuable information.
Data Science Course
Data Science Course in Marathahalli
Data Science Course Training in Bangalore

Inoventic Creative Agency said...

Hello Admin!

Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..

Priyanka said...

Attend The Data Science Courses Bangalore From ExcelR. Practical Data Science Courses Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Courses Bangalore.
Data Science Courses Bangalore
Data Science Interview Questions

training in chennai said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
http://chennaitraining.in/etl-testing-training-in-chennai/
http://chennaitraining.in/java-training-in-chennai/
http://chennaitraining.in/python-training-in-chennai/
http://chennaitraining.in/r-programming-training-in-chennai/
http://chennaitraining.in/salesforce-developer-training-in-chennai/
http://chennaitraining.in/sap-hana-training-in-chennai/

franklinraj said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
http://chennaitraining.in/test-complete-training-in-chennai/
http://chennaitraining.in/load-runner-training-in-chennai/
http://chennaitraining.in/jmeter-training-in-chennai/
http://chennaitraining.in/soapui-testing-training-in-chennai/
http://chennaitraining.in/mobile-application-testing-training-in-chennai/
http://chennaitraining.in/html-training-in-chennai/

franklinraj said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
http://chennaitraining.in/php-training-in-chennai/
http://chennaitraining.in/nodejs-training-in-chennai/
http://chennaitraining.in/reactjs-training-in-chennai/
http://chennaitraining.in/ccnp-training-in-chennai/
http://chennaitraining.in/mcsa-training-in-chennai/
http://chennaitraining.in/mcse-training-in-chennai/

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data science Interview Questions

stevesmith24644 said...

I have scrutinized your blog its engaging and imperative. I like your blog.
custom application development services
Software development company
software application development company
offshore software development company
custom software development company

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
data analytics course in Bangalore

ravali said...

cool stuff you have and you keep overhaul every one of us

Correlation vs Covariance

ravali said...

I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

Simple Linear Regression

Correlation vs Covariance

Dubai Dandb said...

Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

Data Science In Banglore With Placements
Data Science Course In Bangalore
Data Science Training In Bangalore
Best Data Science Courses In Bangalore
Data Science Institute In Bangalore

Thank you..

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

hrithiksai said...

This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science course

EXCELR said...

Nice post. Thanks for sharing information about your services. This is really useful.
Data Science Training in Hyderabad

eCom said...

Thank you, please visit https://www.ecomparemo.com/, thanks!

priyanka said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

priyanka said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

sathishkumar said...

Thank you for this informative blog
python training in bangalore | python online training
aws training in bangalore | aws online training
artificial intelligence training in bangalore | artificial intelligence online training
machine learning training in bangalore | machine learning online training
blockchain training in bangalore | blockchain online training
uipath training in bangalore | uipath online training

EXCELR said...

Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.
Data Science Course in Hyderabad

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

Priyanka said...

Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

Isa Bonita said...

Forex Signals, MT4 and MT5 Indicators, Strategies, Expert Advisors, Forex News, Technical Analysis and Trade Updates in the FOREX IN WORLD

Forex Signals Forex Strategies Forex Indicators Forex News Forex World

datasciencecourse said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.

data science interview questions

datasciencecourse said...

I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

Simple Linear Regression

Correlation vs covariance

KNN Algorithm

hrithiksai said...

This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science course in hyderabad

priyanka said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm

EXCELR said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.Also Checkoutdata science course in Hyderabad

Ontario Security Training said...

Shield Security Solutions Provides Ontario Security Training, Security Guard License or Security License in Ontario. Get Started Today

EXCELR said...

Attend The data science course in Hyderabad From ExcelR. Practical data science course in Hyderabad Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The data science course in Hyderabad. data science course in Hyderabad

datasciencecourse said...

I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

Simple Linear Regression

Correlation vs Covariance

Ontario Security license said...

Shield Security Solutions Offers Security Guard License Training across the province of Ontario. Get Started Today!

Security Guard License | Security License | Ontario Security license | Security License Ontario | Ontario Security Guard License | Security Guard License Ontario

priyanka said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained

Rohini said...

A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
data science training in Hyderabad

priyanka said...

very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Logistic Regression explained

EXCELR said...

Very interesting to read this article.I would like to thank you for the efforts. I also offer Data Scientist Courses data scientist courses

priyanka said...

very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Logistic Regression explained
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm

hrithiksai67 said...

This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science course in hyderabad with placements

Data Science Course IND said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.

Data Science Course in India

sripadojwar1994 said...

ExcelR provides Data Science course . It is a great platform for those who want to learn and become a data scientist. Students are tutored by professionals who have a degree in a particular topic. It is a great opportunity to learn and grow.

Data Science Course
Data science courses
Data scientist certification
Data scientist courses

ashik said...

Python Training in Bangaloer

Deekshitha said...

very informative blog
data science training in Pune

Pravija said...

Very informative content and intresting blog post.Data science course in Nashik