Servlets : Calling init, service and destroy methods explicitly

By Yashwant
Feb 16th, 2014
0 Comments
5257 Views

We all know how servlet lifecycle works. We have methods like init, destroy and service which are lifecycle methods. These methods should be called by framework only and not explicitly by our code. We can override these methods to add our application logic. Framework will call our implemented methods. Also only init and destroy should be overridden. Service method should not be overridden otherwise developer is responsible for consequences. Well, all said and done but technically anything and everything is allowed. You can override service method, call service method from doGet/doPost method, call destroy from init and all weird combinations you can think of. In this post I will try to explain consequences of some of these.

Servlet Life Cycle

Servlets : Calling init, service and destroy explicitly

Calling init from doGet/doPost methods

We are allowed to override init and container will call our overridden method. Though init() is a lifecycle method it can be called from doGet/doPost method. Nothing interesting happens in this case. Any code which you have added in init() method will be executed. There won’t be any exceptions. No cyclic method calls will take place.

Calling destroy from init method

Don’t be shocked, that’s allowed (technically). If destroy() is called from init, on very first request by client for that specific servlet, code written in destroy method will be executed without any side effect.

Calling destroy from doGet/doPost methods

It has the same effect as calling init() method from doGet/doPost method. Code written in destroy method will be executed without any side effect.

For all above scenarios we need to understand one thing – container will call init() only once for the first request. If init/destroy is called from doGet/doPost, invocation will take place for each request.

Calling service from doGet/doPost methods

Last few examples didn’t do much harm but this one is really dangerous. It will cause an infinite loop on server. doGet will call service and service will call doGet again. This cycle will keep on going and server might go for a toss. This generally results in a StackOverflow Exception. So never ever call service method explicitly in your code.

Overloaded init method

There is one more point I would like to mention here which is a bit unrelated to title but a common mistake a developer do while writing servlets. init method is overloaded. init() and init(ServletConfig). This is an example of Template pattern. Developers are advised to override init(). If they override init(ServletConfig) and forget to call super.init(ServletConfig), code will break at runtime. ServletConfig needs to be updated with parent class objects, creating a local config object and not passing it to super will also cause a runtime error.

Incorrect Implementation

ServletConfig config;
public void init(ServletConfig config) throws ServletException {
this.config = config;
}

Correct Implementation

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

Please share your review in comments. Also visit other Java Tutorials. Happy Coding 🙂

About the Author

- An avid reader and a big time comics fan. A software engineer by profession who loves writing code and make things work.

Leave a Reply

Your email address will not be published. Required fields are marked *

facebook comments: