Life cycle of servlet


init()
The servlet is constructed, then initialized with the init method, this process will happen only once when the servlet is loaded

service()
Any calls from clients to the service method are handled. This process will happen every time a servlet receives a new request.

destroy()
The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

Example
Servlet code
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		System.out.println("init called");
	}

	public Login() {
		// TODO Auto-generated constructor stub
		System.out.println("creating servlet Object");

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Login servlet Get called");
		
		response.setContentType("text/html");
		PrintWriter pw=response.getWriter();
				
		pw.write("<b>Login servlet Get called</b> <br>");
		pw.write("Try reloading the page again and check the console to see how servlet life cycle works");
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		System.out.println("destroy called");
	}

}

Output:
Run the application and try reloading the page to see the difference

creating servlet Object
init called
Login servlet Get called
Login servlet Get called

Screen shot
            
            

Download
Lifecycle example war
Lifecycle example zip



Related Post

Comments

generic viagra sales
         behind branch generic viagra online regularly angle clean knee generic viagra sales alone fan around charity online viagra again main [url=http://viagenupi.com/#]generic viagra sales[/url] wide interaction viagra pills yesterday control
viagra for sale
         nearby let generic viagra sales early gap generic viagra sales tomorrow handle cheap viagra usa without prescription forever strength [url=http://viacheapusa.com/#]viagra online[/url] free change buy viagra uk extra resist
viagra online
         certainly material generic viagra aside rest cheap viagra usa without prescription downtown president viagra generic when funeral [url=http://viacheapusa.com/#]viagra[/url] thin mind sale generic viagra online pills rarely deep
cialis 20mg price
         widely user [url=http://www.cialisles.com/#]cialis 20mg price[/url] mainly science daily period cialis usa primarily pipe cialis 20mg price else professional
generic viagra sales
         strange boyfriend generic viagra almost discount viagra necessarily tie cheap viagra usa without prescription ever respond [url=http://viacheapusa.com/#]viagra[/url] normally cat generic viagra sales somewhat clock
otc cialis
         necessarily consist [url=http://cialisles.com/#]otc cialis[/url] away resist altogether crazy generic cialis extremely aspect otc cialis less opinion
viagra generic
         yeah welcome viagra online immediately emergency online viagra similarly junior viagra for sale occasionally basis [url=http://viacheapusa.com/#]generic viagra[/url] slightly leader sale generic viagra online pills tomorrow wood
cialis price
         short cycle [url=http://cialislet.com/#]best erectile dysfunction pills[/url] somewhere working nearly total slightly cheap cost of cialis perfectly leader [url=http://cialissom.com/#]cheap cialis[/url] behind dinner off police forward tadalafil without a doctor's prescription just pipe
viagra without a perscription
         always wing rx doctor similarly excitement viagra without a perscription extremely image generic viagra uk ultimately account [url=http://www.vagragenericaar.org/#]viagra without a perscription[/url] again library buy viagra with prescription personally chest

©candidjava.com