SlideShare a Scribd company logo
3
Most read
5
Most read
12
Most read
Servlet and servlet life cycle
▪ Servlet
▪ Servlet lifecycle diagram
▪ Methods of lifecycle
▪ init()
▪ service()
▪ doGet()
▪ doPost()
▪ destroy()
▪ References
Servlet
❏ Servlet is java class which extends the functionality of web server
by dynamically generating web pages.
❏ Servlet technology is used to create Dynamic web application
❏ Servlet technology is robust and scalable .
❏ Before Servlet, Common Gateway Interface scripting language was
popular as a server-side programming language.
Servlet Life Cycle
Servlet Life Cycle: init()
Servlet class is loaded
The class loader is responsible to load the servlet class.
The servlet class is loaded when the first request for the servlet is received by the web container.
Servlet instance is created
The web container creates the instance of a servlet after loading the servlet class.
The servlet instance is created only once in the servlet life cycle.
init method is invoked
The web container calls the init method only once after creating the servlet instance.
The init method is used to initialize the servlet.
Servlet Life Cycle: init()
Syntax:
public void init(ServletConfig config) throws ServletException
{
//initialization…
}
Servlet Life Cycle: Service()
▪ The service() method is the main method to perform the actual task.
▪ The servlet container calls the service() method to handle requests
coming from the client and to write the response back to the client.
▪ Each time the server receives a request for a servlet, the server spawns a
new thread and calls service.
Servlet Life Cycle: Service()
Syntax:
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
…
}
Servlet Life Cycle: Service()
▪ The service() method checks the HTTP request type (GET, POST,
PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc.
methods as appropriate.
▪ The doGet() and doPost() are most frequently used methods with in
each service request.
Service: doGet()
▪ A GET request results from request for a URL or from an HTML form,
should be handled by doGet() method.
Syntax:
public void doGet
(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
// Servlet code …
}
Service: doPost()
▪ A POST request results from an HTML form that specifically lists
POST as the METHOD and it should be handled by doPost() method.
Syntax:
public void doPost (HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
// Servlet code …
}
doGet() vs doPost()
Application
▪ doGet() shall be used when small amount of data and insensitive
data like a query has to be sent as a request.
▪ doPost() shall be used when comparatively large amount of
sensitive data has to be sent.
E.g.
Sending data after filling up a form or sending login & password.
doGet() & doPost()
Servlet Life Cycle: Destroy()
▪ The destroy() method is called only once at the end of the life cycle of a
servlet.
▪ 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.
▪ After the destroy() method is called, the servlet object is marked for
garbage collection.
Servlet Life Cycle: Destroy()
Syntax :
public void destroy()
{
// Finalization code...
}
References
▪
▪
Servlet and servlet life cycle

More Related Content

What's hot (20)

PPS
Java rmi
kamal kotecha
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PDF
Remote Method Invocation (RMI)
Peter R. Egli
 
PPTX
MULTI THREADING IN JAVA
VINOTH R
 
PPTX
Http Server Programming in JAVA - Handling http requests and responses
bharathiv53
 
PPTX
Advance Java Topics (J2EE)
slire
 
PPTX
World wide web architecture presentation
ImMe Khan
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPT
Java interfaces
Raja Sekhar
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
PPT
Servlets
Sasidhar Kothuru
 
PPT
Jsp ppt
Vikas Jagtap
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PPTX
Chapter 3 servlet & jsp
Jafar Nesargi
 
PPS
Wrapper class
kamal kotecha
 
PPTX
Client side scripting and server side scripting
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
JAVA AWT
shanmuga rajan
 
PPSX
JDBC: java DataBase connectivity
Tanmoy Barman
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
Java rmi
kamal kotecha
 
Java Server Pages(jsp)
Manisha Keim
 
Remote Method Invocation (RMI)
Peter R. Egli
 
MULTI THREADING IN JAVA
VINOTH R
 
Http Server Programming in JAVA - Handling http requests and responses
bharathiv53
 
Advance Java Topics (J2EE)
slire
 
World wide web architecture presentation
ImMe Khan
 
Java I/o streams
Hamid Ghorbani
 
Java interfaces
Raja Sekhar
 
Java Streams
M Vishnuvardhan Reddy
 
Jsp ppt
Vikas Jagtap
 
Network programming in java - PPT
kamal kotecha
 
Chapter 3 servlet & jsp
Jafar Nesargi
 
Wrapper class
kamal kotecha
 
Client side scripting and server side scripting
baabtra.com - No. 1 supplier of quality freshers
 
JAVA AWT
shanmuga rajan
 
JDBC: java DataBase connectivity
Tanmoy Barman
 
Exception Handling in JAVA
SURIT DATTA
 

Similar to Servlet and servlet life cycle (20)

PPTX
SevletLifeCycle
Chandnigupta80
 
PPTX
LIFE CYCLE OF SERVLET
keerthikaA8
 
PPTX
Servlet session 2
Anuj Singh Rajput
 
PDF
servlet_lifecycle.pdf
DrKowsalyaSaravanan
 
PPTX
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
PPTX
Servlet and Servlet Life Cycle
Dhrumil Panchal
 
PPTX
Servlets & jdbc
Siva Priya
 
PPT
Servlet lifecycle
chauhankapil
 
PPTX
Servlets
Bala Murugan
 
RTF
Servlet lifecycle
Durga Devi Thulluri
 
PPT
Servlet ppt by vikas jagtap
Vikas Jagtap
 
PPTX
Servlet_Life_cycle has details about the phases.pptx
juhishrivastava25
 
PPTX
java Servlet_Life_Cycle_Notes with basic level
LOKESHY6
 
PPTX
ajava unit 1.pptx
PawanKumar617960
 
PPT
S E R V L E T S
patinijava
 
PDF
Servlet Life Cycle & Generic Servlet
DianaDevi8
 
PPTX
Introduction to Servlet.pptx
DrKowsalyaSaravanan
 
PPTX
J servlets
reddivarihareesh
 
PPTX
SERVLET in web technolgy engineering.pptx
ARUNKUMARM230658
 
PPTX
Enterprise java unit-1_chapter-3
sandeep54552
 
SevletLifeCycle
Chandnigupta80
 
LIFE CYCLE OF SERVLET
keerthikaA8
 
Servlet session 2
Anuj Singh Rajput
 
servlet_lifecycle.pdf
DrKowsalyaSaravanan
 
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
Servlet and Servlet Life Cycle
Dhrumil Panchal
 
Servlets & jdbc
Siva Priya
 
Servlet lifecycle
chauhankapil
 
Servlets
Bala Murugan
 
Servlet lifecycle
Durga Devi Thulluri
 
Servlet ppt by vikas jagtap
Vikas Jagtap
 
Servlet_Life_cycle has details about the phases.pptx
juhishrivastava25
 
java Servlet_Life_Cycle_Notes with basic level
LOKESHY6
 
ajava unit 1.pptx
PawanKumar617960
 
S E R V L E T S
patinijava
 
Servlet Life Cycle & Generic Servlet
DianaDevi8
 
Introduction to Servlet.pptx
DrKowsalyaSaravanan
 
J servlets
reddivarihareesh
 
SERVLET in web technolgy engineering.pptx
ARUNKUMARM230658
 
Enterprise java unit-1_chapter-3
sandeep54552
 
Ad

Recently uploaded (20)

PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Hashing Introduction , hash functions and techniques
sailajam21
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
Ad

Servlet and servlet life cycle

  • 2. ▪ Servlet ▪ Servlet lifecycle diagram ▪ Methods of lifecycle ▪ init() ▪ service() ▪ doGet() ▪ doPost() ▪ destroy() ▪ References
  • 3. Servlet ❏ Servlet is java class which extends the functionality of web server by dynamically generating web pages. ❏ Servlet technology is used to create Dynamic web application ❏ Servlet technology is robust and scalable . ❏ Before Servlet, Common Gateway Interface scripting language was popular as a server-side programming language.
  • 5. Servlet Life Cycle: init() Servlet class is loaded The class loader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container. Servlet instance is created The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle. init method is invoked The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet.
  • 6. Servlet Life Cycle: init() Syntax: public void init(ServletConfig config) throws ServletException { //initialization… }
  • 7. Servlet Life Cycle: Service() ▪ The service() method is the main method to perform the actual task. ▪ The servlet container calls the service() method to handle requests coming from the client and to write the response back to the client. ▪ Each time the server receives a request for a servlet, the server spawns a new thread and calls service.
  • 8. Servlet Life Cycle: Service() Syntax: public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { … }
  • 9. Servlet Life Cycle: Service() ▪ The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate. ▪ The doGet() and doPost() are most frequently used methods with in each service request.
  • 10. Service: doGet() ▪ A GET request results from request for a URL or from an HTML form, should be handled by doGet() method. Syntax: public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { // Servlet code … }
  • 11. Service: doPost() ▪ A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method. Syntax: public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code … }
  • 13. Application ▪ doGet() shall be used when small amount of data and insensitive data like a query has to be sent as a request. ▪ doPost() shall be used when comparatively large amount of sensitive data has to be sent. E.g. Sending data after filling up a form or sending login & password. doGet() & doPost()
  • 14. Servlet Life Cycle: Destroy() ▪ The destroy() method is called only once at the end of the life cycle of a servlet. ▪ 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. ▪ After the destroy() method is called, the servlet object is marked for garbage collection.
  • 15. Servlet Life Cycle: Destroy() Syntax : public void destroy() { // Finalization code... }