TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Servlets

Java Servlet Lifecycle

Overview

The Java Servlet interface defines Servlet lifecycle methods to initialize the servlet, service requests, and remove the servlet from the server container. These are invoked in the following sequence:

  • init()
  • service(req,res)
  • destroy()

 

Java Servlet Deployment

The first client request should wait until the servlet object is created and initialized by the web container to handle the request.  To avoid this we can also inform the servlet container to create the servlet object when the server starts.

When the servlet object is created the container initializes the servlet object by invoking the constructor. After the constructor, the lifecycle init() method is invoked.

init()

The init method is invoked by the web container to indicate that the servlet can handle service requests. The signature of the init method is as follows:

public void init(ServletConfig config) throws ServletException

This method is called after instantiating the servlet. The servlet is created and then initialized with the init() method. The web container creates the servlet object whenever the first request is given to the servlet.

 

 

Java Servlet LifeCycle

service()

The service method is invoked by the servlet container that allows the servlet to accept the request and respond with a response. The method signature of the service method is as follows:

public void service(ServletRequest req,ServletResponse res)
throws ServletException, IOException

Once the servlet is initialized, it will be able to process the client’s request.  The response is provided back to the client after processing the request. The service() method would be executed for each client request.

Servlets that run under the multithreaded web containers can handle multiple service requests simultaneously. Any calls from the clients to the service() method are handled.

destroy()

The servlet is then destroyed with the destroy() method and sent for garbage collection. The method signature of the destroy method is as follows:

public void destroy()

The init() and destroy() methods are executed only once during the lifecycle of the Servlet. The destroy() method is invoked by the container whenever the server is shut down or when the servlet is removed( servlet undeploy)  from the servlet.

 

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Java Servlets /

JSP Architecture

Java Servlets /

Java ServletRequest Interface

Model View Controller

Java Servlets /

Model View Controller( MVC )

Java Servlets /

Deployment Descriptor File (web.xml)

Java Servlets /

GenericServlet Class

‹ Java Servlet API› Create a Java Servlet

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com