05/02/25 ------------ Q)What is a servlet? =>A Servlet Container managed public Java class that implements Servlet interface is nothing but a servlet. Q)What is the general structure of a servlet? public class MyServlet implements Servlet{ public void init(ServletConfig sc){ //resources allocation for eg. database connection } public void service(ServletRequest request,ServletResponse response){ //serving the client request(5 duties) } public void destroy(){ //resources deallocation for eg. closing the database connection } ...... } Q)What is the life cycle of a servlet? =>Servlet Container controls the life cycle of a servlet. =>3 life cycle methods and 4 life cycle phases describe the life cycle of a servlet. =>Life cycle methods are 1)init() 2)service() 3)destroy() =>Life cycle phases are 1)instantiation phase 2)initialization phase 3)servicing phase 4)destruction phase Instantiation phase --------------------- Servlet Container creating the instance of a servlet class is nothing but instantiation phase of the servlet. initialization phase ------------------- =>During instnatiation phase, servlet instance is there but it can't serve the client request. =>Servlet Container calling the init method on the servlet instance is nothing but the initialization phase of the servlet. =>Servlet Container calls init method only once in the life time of a servlet. =>Once init method is completely executed, servlet instance is ready to serve the client request. =>Programmer can provide resources required for the servlet in init method. destruction phase -------------------- =>Servlet Container calling destroy() method on servlet instance is nothing but destruction phase. =>Servlet Container calls destroy method only once in the life time of a servlet. =>Programmer can release the resources given to the servlet here. For eg. database connection closing