What are the methods being used in a servlet to initialize and destroy it?
Hello my friends , I have completed my graduation , but since i had opted for web development when I was asked about the electives being taken in my fifth semesters , but now I want to learn he Java language . I had learned HTML and ASP in he web development , but in java there is lot of things to be learned on of my friend who was senior to me had gone for an interview he was asked a lot about the Java and was asked about the methods being asked in the Servlet lifecycle , can any one please tell me what he actually meant by that , I did not get anything from that question , and I am a bit worried what will happen if I appear for any interview and been thrown such questions , if so I will go black and will not clear any of the Java relevant interview.
Re: What are the methods being used in a servlet to initialize and destroy it?
Most of the students who choose web development over java tend to face this issue as they just look at the previous scores and see that students have scored more marks in the web development over java and then opt for it and later on know th importance of the java, nevertheless you had asked about the methods , I can name them they are init method , service method and the last method is destroy method.
Re: What are the methods being used in a servlet to initialize and destroy it?
The life cycle begins when container creates an object of servlet class and invokes the the init() method, and terminates with the container invoking the destroy() method.
The syntax of these methods is shown as below of this methods are shown below :
Following is the syntax of the init method
Code:
public void init(ServletConfig con) throws ServletException<br />
public void service(ServletRequest request , ServletResponse response ) throws ServletException, IOException
Following is the syntax of the destroy method
Code:
public void destroy()
Re: What are the methods being used in a servlet to initialize and destroy it?
The servlet life cycle consists of four steps, instantiation, initialization, handling of a particular request and then that particular is ended .During initial procedure , web container actually loads the servlet class and generates a new object of the servlet. The container can generate a servlet object at container startup or it can holdup it until the servlet is required to service a request. During initialization period of the Servlet life cycle, the web container begins the servlet instance by invoking the init() method. That container passes an instance implementing the ServletConfig interface with the help of the init() function . This configuration instance permits the servlet the right to use name-value initialization arguments from the web application
Re: What are the methods being used in a servlet to initialize and destroy it?
The servlet saves a message into server log file when servlet is initialized and initializes the counter variable to 0. Each time the service function is invoked , it increases the counter by 1 and shows the present value of counter to user. At last when destroy method is invoked , it stores a message to server log file. Thus a init function initializes , service method does the actual function and the destroy method destroys or terminates the actual instance of the servlet.