• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Servet- Life Cycle

December 31, 2010 By j2eereference 1 Comment

 

 Servlet life cycle defines how a servlet is loaded, instantiated, initialized and handles requests from the clients.

Loading and Instantiating: –

Loading and instantiation can occur when the container is started. When the servlet engine is started, the servlet container loads the servlet class using normal Java class loading facilities. After loading the Servlet class, the container instantiates it

Initialization :-

After the class is Loaded and Instantiated, the servlet engines initalize it by calling the following method:

public void init(ServletConfig config)  throws ServletException

In the method init, the config object is an instance of ServletConfig class. The config object contains initialization parameters and a ServletContext instance. The initialization parameters are specific to each servlet and configured in the deployment descriptor as part of the definition of the servlet.

Initialization parameter to the servlet is defined in the deployment descriptor file web.XML like following:

1
2
3
4
5
6
7
8
9
10
<servlet>
<init-param>
    <param-name>Emp_id</param-name>
    <param-value>1</param-value>
</init-param>
<init-param>
    <param-name>Emp_name</param-name>
    <param-value>John</param-value>
</init-param>
</servlet>

Processing Request :-

 After initialization, the servlet is able to handle client requests.

Destroying :-

The servlet engine stops a servlet by invoking the servlet’s destroy() method. The destroy() method runs only one time during the lifetime of the servlet and this is the end of the servlet.

After a servlet’s destroy() method is invoked, the servlet engine unloads the servlet, and the Java virtual machine eventually performs garbage collection on the memory resources associated with the servlet.

Following three methods are known as servlet life cycle methods.

 1)           public void init(ServletConfig config) throws ServletException

 2)           public void doGet(HttpServletRequest req, HttpServletResponse rsp)  throws ServletException, IOException

                OR

               public void doPost(HttpServletRequest req, HttpServletResponse rsp)  throws ServletException, IOException 

3)            public void destroy()

Filed Under: Servlets

Reader Interactions

Comments

  1. Sau Gambold says

    May 29, 2011 at 11:19 pm

    It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.