• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » introduction » Java Servlet Life Cycle
Next →
← Prev

Java Servlet Life Cycle

By Dinesh Thakur

Java Servlet life cycle consists of a series of events that begins when the Servlet container loads Servlet, and ends when the container is closed down Servlet. A servlet container is the part of a web server or an application server that controls a Servlet by managing its life cycle. Basically there are three phases of the life cycle.

First, the Servlet container loading Servlet in the memory, creating a servlet object and initializes it. Second, Servlet object services requests mapped it by the Servlet container. Thirdly Servlet container shuts down Servlet and Java Virtual Machine (JVM), which is the environment that runs Java applications, freeing the computer resources that the Servlet.

Three of the five methods defined by the Servlet interface — init (), service () and destroy () —‘s life cycle methods.

We’ll be covering the following topics in this tutorial:

  • The init() method:
  • The service () method
  • The destroy () method
  • Architecture Diagram of Servlet Life Cycle:

The init() method:

The Servlet container running a Servlet’s init () method, which initializes Servlet, once in the Servlet life cycle, after loading the Servlet and create a Servlet object. The init method definition looks like this:

public void init(ServletConfig config) throws ServletException {
  // Initialization code...
}

 

 

 

 

 

 

When instantiating the servlet container passes an argument to the init() method a ServletConfig object for loading specific configuration settings to the servlet.

If an error occurs upon calling the init () method, it throws an exception of type and ServletException servlet is not initialized.

The service () method

The container runs the Servlet service () method for each request from a client, a web browser. The service method definition looks like this:

public void service(ServletRequest request, 
                    ServletResponse response)
      throws ServletException, IOException{
}

 

 

 

 

 

 

 

 

The ServletRequest and ServletResponse objects are automatically passed as a parameter to the service () method by the container.

The destroy () method

The Servlet container running a servlet’s destroy () method once in a lifetime to close the Servlet. The destroy method definition looks like this:

public void destroy() { 
    // Finalization code...
  }

 

 

 

 

 

 

 

The destroy ( ) method waits until all threads started by the service () method before terminating the execution of the servlet.

Note– The init() and destroy() method only once called during its servlet life cycle.

Architecture Diagram of Servlet Life Cycle:

                     Architecture Diagram of Servlet Life Cycle

 

The servlet is loaded at server startup or when the first request. The servlet is instantiated by the server. The init ( ) method is invoked by the container. In the first application, the container creates specific Request and Response objects to the query.

The service () method is called for each request in a new thread. The Request and Response objects are passed as parameters. Through the Request object, the service () method will be able to analyze information from the client With the Response object, the service () method will provide a response to the client.

This method can be executed by multiple threads; there should be exclusion for the use of certain resources processes.

The destroy () method is called when unloading the servlet, that is to say when it is no longer required by the server. The servlet is then reported to the garbage collector.

You’ll also like:

  1. What is the Servlet container raises the life cycle events in servlet program?
  2. Applet Life Cycle in Java with Example
  3. Thread Life Cycle in Java
  4. Life Cycle of JSP
  5. Explain Applet Life Cycle
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW