SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
▪ 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

Contenu connexe

Tendances

Java servlets
Java servletsJava servlets
Java servlets
lopjuan
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 

Tendances (20)

JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java servlets and CGI
Java servlets and CGIJava servlets and CGI
Java servlets and CGI
 
Java servlets
Java servletsJava servlets
Java servlets
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Servlets
ServletsServlets
Servlets
 
Generics
GenericsGenerics
Generics
 
Applets
AppletsApplets
Applets
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Packages in java
Packages in javaPackages in java
Packages in java
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Servlets
ServletsServlets
Servlets
 
Java Beans
Java BeansJava Beans
Java Beans
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
 
Jsp lifecycle
Jsp   lifecycleJsp   lifecycle
Jsp lifecycle
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 

Similaire à Servlet and servlet life cycle

Dynamic content generation
Dynamic content generationDynamic content generation
Dynamic content generation
Eleonora Ciceri
 

Similaire à Servlet and servlet life cycle (20)

SevletLifeCycle
SevletLifeCycleSevletLifeCycle
SevletLifeCycle
 
Dynamic content generation
Dynamic content generationDynamic content generation
Dynamic content generation
 
servlet_lifecycle.pdf
servlet_lifecycle.pdfservlet_lifecycle.pdf
servlet_lifecycle.pdf
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Servlets
ServletsServlets
Servlets
 
Servlets
ServletsServlets
Servlets
 
Servlet
Servlet Servlet
Servlet
 
Java web application development
Java web application developmentJava web application development
Java web application development
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdf
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
LIFE CYCLE OF SERVLET
LIFE CYCLE OF SERVLETLIFE CYCLE OF SERVLET
LIFE CYCLE OF SERVLET
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 
Java servlets
Java servletsJava servlets
Java servlets
 
WEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES ServletWEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES Servlet
 
Session 26 - Servlets Part 2
Session 26 - Servlets Part 2Session 26 - Servlets Part 2
Session 26 - Servlets Part 2
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 

Dernier

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Dernier (20)

Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 

Servlet and servlet life cycle

  • 1.
  • 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... }