SlideShare a Scribd company logo
1 of 11
SESSION – 2
By→ Anuj Kumar Singh
Servlet Life Cycle
1. A servlet life cycle can be defined as the entire process from its
creation till the destruction.
2. The following are the paths followed by a servlet.
a. The servlet is initialized by calling the init() method.
b. The servlet calls service() method to process a client's request.
c. The servlet is terminated by calling the destroy() method.
d. Finally, servlet is garbage collected by the garbage collector of the
JVM.
1. The init() Method :
a. The init method is called only once. It is called only when the
servlet is created, and not called for any user requests afterwards.
So, it is used for one-time initializations.
b. The servlet is normally created when a user first invokes a URL
corresponding to the servlet, but you can also specify that the
servlet be loaded when the server is first started.
c. When a user invokes a servlet, a single instance of each servlet
gets created, with each user request resulting in a new thread that
is handed off to doGet or doPost as appropriate. The init() method
simply creates or loads some data that will be used throughout the
life of the servlet.
d. public void init() throws ServletException { // Initialization code... }
2. The service() Method
1. The service() method is the main method to perform the actual task.
The servlet container (i.e. web server) calls the service() method to
handle requests coming from the client (browsers) and to write the
formatted response back to the client.
2. Each time the server receives a request for a servlet, the server
spawns a new thread and calls service. The service() method checks
the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls
doGet, doPost, doPut, doDelete, etc. methods as appropriate.
3. So you have nothing to do with service() method but you override
either doGet() or doPost() depending on what type of request you
receive from the client. The doGet() and doPost() are most frequently
used methods.
public void service(ServletRequest request, ServletResponse
response) throws ServletException,IOException
{// Initialization code... }
1. The doGet() Method
A GET request results from a normal request for a URL or from an
HTML form that has no METHOD specified and it should be handled
by doGet() method.
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{ // Servlet code }
2. The doPost() Method
A POST request results from an HTML form that specifically lists
POST as the METHOD and it should be handled by doPost()
method.
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{ // Servlet code }
3. The destroy() Method
a. The destroy() method is called only once at the end of the life cycle
of a servlet.
b. This method gives your servlet a chance to close database
connections, halt background threads, write cookie lists or hit
counts to disk, and perform other such cleanup activities.
c. After the destroy() method is called, the servlet object is marked for
garbage collection.
d. public void destroy()
{ // Finalization code... }
Architecture Diagram
1. First the HTTP requests coming to the
server are delegated to the servlet
container.
2. The servlet container loads the servlet
before invoking the service() method.
3. Then the servlet container handles
multiple requests by spawning
multiple threads, each thread
executing the service() method of a
single instance of the servlet.
How Servlet works?
1. It is important to learn how servlet works for understanding the
servlet well. Here, we are going to get the internal detail about the
first servlet program.
2. The server checks if the servlet is requested for the first time.
3. If yes, web container does the following tasks:
a. loads the servlet class.
b. instantiates the servlet class.
c. calls the init method passing the ServletConfig object
4. else
calls the service method passing request and response objects
5. The web container calls the destroy method when it needs to
remove the servlet such as at time of stopping server or undeploying
the project.
How web container handles the servlet request?
1. The web container is responsible to handle the request. Let's see
how it handles the request.
2. maps the request with the servlet in the web.xml file.
3. creates request and response objects for this request
4. calls the service method on the thread
5. The public service method internally calls the protected service
method
6. The protected service method calls the doGet method depending
on the type of request.
7. The doGet method generates the response and it is passed to the
client.
8. After sending the response, the web container deletes the request
and response objects. The thread is contained in the thread pool or
deleted depends on the server implementation.
Servlet   session 2

More Related Content

Similar to Servlet session 2

S E R V L E T S
S E R V L E T SS E R V L E T S
S E R V L E T S
patinijava
 

Similar to Servlet session 2 (20)

servlet_lifecycle.pdf
servlet_lifecycle.pdfservlet_lifecycle.pdf
servlet_lifecycle.pdf
 
Enterprise java unit-1_chapter-3
Enterprise java unit-1_chapter-3Enterprise java unit-1_chapter-3
Enterprise java unit-1_chapter-3
 
ajava unit 1.pptx
ajava unit 1.pptxajava unit 1.pptx
ajava unit 1.pptx
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 
Servlet lifecycle
Servlet  lifecycleServlet  lifecycle
Servlet lifecycle
 
S E R V L E T S
S E R V L E T SS E R V L E T S
S E R V L E T S
 
Servlet lifecycle
Servlet lifecycleServlet lifecycle
Servlet lifecycle
 
Servlets
ServletsServlets
Servlets
 
WEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES ServletWEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES Servlet
 
Adv java unit 4 M.Sc CS.pdf
Adv java unit 4 M.Sc CS.pdfAdv java unit 4 M.Sc CS.pdf
Adv java unit 4 M.Sc CS.pdf
 
J servlets
J servletsJ servlets
J servlets
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Servlets
ServletsServlets
Servlets
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to Servlet.pptx
Introduction to Servlet.pptxIntroduction to Servlet.pptx
Introduction to Servlet.pptx
 
SevletLifeCycle
SevletLifeCycleSevletLifeCycle
SevletLifeCycle
 
Servlets lecture2
Servlets lecture2Servlets lecture2
Servlets lecture2
 
J2EE-assignment
 J2EE-assignment J2EE-assignment
J2EE-assignment
 
J2ee servlet
J2ee servletJ2ee servlet
J2ee servlet
 

More from Anuj Singh Rajput

More from Anuj Singh Rajput (20)

Web technology
Web technologyWeb technology
Web technology
 
Java script
Java scriptJava script
Java script
 
Html (hypertext markup language)
Html (hypertext markup language)Html (hypertext markup language)
Html (hypertext markup language)
 
Css
CssCss
Css
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
Jsp session 13
Jsp   session 13Jsp   session 13
Jsp session 13
 
Jsp session 12
Jsp   session 12Jsp   session 12
Jsp session 12
 
Jsp session 11
Jsp   session 11Jsp   session 11
Jsp session 11
 
Jsp session 10
Jsp   session 10Jsp   session 10
Jsp session 10
 
Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
 
Jsp session 8
Jsp   session 8Jsp   session 8
Jsp session 8
 
Jsp session 7
Jsp   session 7Jsp   session 7
Jsp session 7
 
Jsp session 6
Jsp   session 6Jsp   session 6
Jsp session 6
 
Jsp session 5
Jsp   session 5Jsp   session 5
Jsp session 5
 
Jsp session 4
Jsp   session 4Jsp   session 4
Jsp session 4
 
Jsp session 3
Jsp   session 3Jsp   session 3
Jsp session 3
 
Jsp session 2
Jsp   session 2Jsp   session 2
Jsp session 2
 
Jsp session 1
Jsp   session 1Jsp   session 1
Jsp session 1
 
Servlet session 14
Servlet   session 14Servlet   session 14
Servlet session 14
 
Servlet session 13
Servlet   session 13Servlet   session 13
Servlet session 13
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Servlet session 2

  • 1. SESSION – 2 By→ Anuj Kumar Singh
  • 2. Servlet Life Cycle 1. A servlet life cycle can be defined as the entire process from its creation till the destruction. 2. The following are the paths followed by a servlet. a. The servlet is initialized by calling the init() method. b. The servlet calls service() method to process a client's request. c. The servlet is terminated by calling the destroy() method. d. Finally, servlet is garbage collected by the garbage collector of the JVM.
  • 3. 1. The init() Method : a. The init method is called only once. It is called only when the servlet is created, and not called for any user requests afterwards. So, it is used for one-time initializations. b. The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started. c. When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply creates or loads some data that will be used throughout the life of the servlet. d. public void init() throws ServletException { // Initialization code... }
  • 4. 2. The service() Method 1. The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client (browsers) and to write the formatted response back to the client. 2. Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate. 3. So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client. The doGet() and doPost() are most frequently used methods.
  • 5. public void service(ServletRequest request, ServletResponse response) throws ServletException,IOException {// Initialization code... } 1. The doGet() Method A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code }
  • 6. 2. The doPost() Method A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code }
  • 7. 3. The destroy() Method a. The destroy() method is called only once at the end of the life cycle of a servlet. b. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities. c. After the destroy() method is called, the servlet object is marked for garbage collection. d. public void destroy() { // Finalization code... }
  • 8. Architecture Diagram 1. First the HTTP requests coming to the server are delegated to the servlet container. 2. The servlet container loads the servlet before invoking the service() method. 3. Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.
  • 9. How Servlet works? 1. It is important to learn how servlet works for understanding the servlet well. Here, we are going to get the internal detail about the first servlet program. 2. The server checks if the servlet is requested for the first time. 3. If yes, web container does the following tasks: a. loads the servlet class. b. instantiates the servlet class. c. calls the init method passing the ServletConfig object 4. else calls the service method passing request and response objects 5. The web container calls the destroy method when it needs to remove the servlet such as at time of stopping server or undeploying the project.
  • 10. How web container handles the servlet request? 1. The web container is responsible to handle the request. Let's see how it handles the request. 2. maps the request with the servlet in the web.xml file. 3. creates request and response objects for this request 4. calls the service method on the thread 5. The public service method internally calls the protected service method 6. The protected service method calls the doGet method depending on the type of request. 7. The doGet method generates the response and it is passed to the client. 8. After sending the response, the web container deletes the request and response objects. The thread is contained in the thread pool or deleted depends on the server implementation.