Wednesday 21 August 2013

Can we call init method in service method of servlet?

This is a very tricky question asked in an interview. The servlet lifecycle is a very important question in any java interview. The goal behind asking this question is to check the knowledge of servlet lifecycle of the candidate. This question can be modified in many ways like can we call destroy method in service method etc. (Read the link --http://javaitblog.blogspot.in/2013/04/what-happens-if-you-call-destroy-from.html). 
For the question “can we call init method in service method”, the direct answer is “Yes”. We can call init method in service method for any number of times. The init method initializes the ServletConfig parameter or initializes the servlet instance. The init method will be called by the container only once when it is started. So there is no use of calling it again in the service method but if we want, we can call it in the service method. To understand it more clearly, please see the below example.
Example Code:
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/demoCalInitMethod")
public class demoCalInitMethod extends HttpServlet {
                private static final long serialVersionUID = 1L;
                ServletConfig config;
                int count=0;
                  
    public demoCalInitMethod() {
        super();
    }

                public void init(ServletConfig config) throws ServletException {
                                this.config = config;
                                ++count;
                }
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
               
                                System.out.println("The value of count variable before calling init methods in service method :- "+count);
                                init(config);
                                init(config);
                                init(config);
                                init(config);
                                init(config);
                                System.out.println("The value of count variable before calling init methods in service method :- "+count);
                }

                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                                                }

                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                }

}

Output :-

The value of count variable before calling init methods in service method :- 1
The value of count variable before calling init methods in service method :- 6

In the above example, the init method is called in service method for five times. In init method, we incremented an integer type variable “count” with one so whenever this method is called it will increment the value of “count” variable by one. We printed the value of count variable in service method before and after calling the init method explicitly. As the output showing above, it printed the value of count 1 before calling the init methods explicitly (Because container calls it once automatically when it loads the servlet) and 6 after calling  the init methods explicitly because  we called the init method for five times in service method.
So init will initiate the servlet n times if call the init method n times with same ServletConfig object.

No comments:

Post a Comment