SlideShare a Scribd company logo
1 of 24
Submitted by
K.Priyadarsini
N.Pandimeena
V.Sarmila
IIM.SC(CS&IT)
Nadar saraswathi college of arts & science ,
Theni.
SERVLET:
 Servlets provide a component-based, platform-
independent method for building Web based applications,
without the performance limitations of CGI programs.
Servlets have access to the entire family of Java APIs,
including the JDBC API to access enterprise databases.
 Java Servlets are programs that run on a Web or
Application server and act as a middle layer between a
requests coming from a Web browser or other HTTP
client and databases or applications on the HTTP server.
Java Servlets often serve the same purpose as
programs implemented using the Common Gateway
Interface (CGI). But Servlets offer several advantages in
comparison with the CGI.
 Performance is significantly better.
 Servlets execute within the address space of a Web
server. It is not necessary to create a separate process to
handle each client request.
 Servlets are platform independent because they are
written in Java.
 Java security manager on the server enforces a set of
restrictions to protect the resources on a server machine.
So servlets are trusted.
Servlet interface between client and server:
CGI(Common Gateway Interface):
CGI technology enables the web server to call
an external program and pass HTTP request information to
the external program to process the request. For each
request, it starts a new process.
Advantage of servlet:
 Better performance:
Because it creates a thread for each request not
process.
 Portability:
Because it uses java language.
 Robust:
Servlets are managed by JVM so we don't need
to worry about memory leak, garbage collection etc.
 Secure:
Because it uses java language..
Website:
 Website is a collection of related web pages that may
contain text, images, audio and video. The first page of a
website is called home page. Each website has specific
internet address (URL) that you need to enter in your
browser to access a website.
 Website is hosted on one or more servers and can be
accessed by visiting its homepage using a computer
network. A website is managed by its owner that can be an
individual, company or an organization.
HTTP:
 The Hypertext Transfer Protocol (HTTP) is
application-level protocol for collaborative, distributed,
hypermedia information systems. It is the data
communication protocol used to establish communication
between client and server.
 HTTP is TCP/IP based communication protocol,
which is used to deliver the data like image files, query
results, HTML files etc on the World Wide Web (WWW)
with the default port is TCP 80. It provides the
standardized way for computers to communicate with each
other.
The Basic Architecture of HTTP:
Servlet API:
 The javax.servlet and javax.servlet.http packages
represent interfaces and classes for servlet API.
 The javax.servlet package contains many interfaces
and classes that are used by the servlet or web container.
These are not specific to any protocol.
 The javax.servlet.http package contains interfaces
and classes that are responsible for http requests only.
javax.servlet:
The javax.servlet package contains a number of
classes and interfaces that describe and define the contracts
between a servlet class and the runtime environment
provided for an instance of such a class by a conforming
servlet container.
javax.servlet.http:
The javax.servlet.http package contains a
number of classes and interfaces that describe and define the
contracts between a servlet class running under the HTTP
protocol and the runtime environment provided for an
instance of such a class by a conforming servlet container.
Classes in javax.servlet package:
Generic Servlet
Servlet Input Stream
Servlet Output Stream
Servlet Request Wrapper
Servlet Response Wrapper
Servlet Request Event
Servlet Context Event
Servlet Request Attribute Event
Servlet Context Attribute Event
Servlet Exception
Unavailable Exception
Classes in javax.servlet.http package:
Http Servlet
Cookie
Http Servlet Request Wrapper
Http Servlet Response Wrapper
Http Session Event
Http Session Binding Event
Http Utils (deprecated now)
Http Servlet class:
 The Http Servlet class extends the Generic Servlet
class and implements Serializable interface. It provides
http specific methods such as doGet , doPost , doHead ,
doTrace etc.
Life Cycle of a Servlet (Servlet Life Cycle):
 The web container maintains the life cycle of a servlet
instance. Let's see the life cycle of the servlet:
Servlet class is loaded.
Servlet instance is created.
init method is invoked.
service method is invoked.
destroy method is invoked.
Diagram of servlet life cycle:
 As displayed in the above diagram, there are three
states of a servlet:
New
Ready
End
 The servlet is in new state if servlet instance is
created. After invoking the init() method, Servlet comes in
the ready state. In the ready state, servlet performs all the
tasks. When the web container invokes the destroy()
method, it shifts to the end state.
Written inside the public service method:
The public service method converts the Servlet
Request object into the Http Servlet Request type and Servlet
Response object into the Http Servlet Response type.
Then, calls the service method passing these
objects.
PROGRAM:
public void service(Servlet Request req , Servlet Response res)
throws Servlet Exception, IOException
{
Http Servlet Request request;
Http Servlet Response response;
try
{
request = (Http Servlet Request)req;
response = (Http Servlet Response)res;
}
catch(Class Cast Exception e)
{
throw new Servlet Exception("non HTTP request or response");
}
service(request, response);
}
Written inside the protected service method:
The protected service method checks the type
of request, if request type is get, it calls doGet method, if
request type is post, it calls doPost method, so on.
Let's see the internal code:
PROGRAM:
protected void service(HttpServletRequest req, HttpServlet
Response resp)
throws ServletException, IOException
{
String method = req . getMethod();
if(method . equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
}
....
}
}
Servlet in java , java servlet , servlet servlet and CGI, API

More Related Content

What's hot

What's hot (20)

An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
 
Listeners and filters in servlet
Listeners and filters in servletListeners and filters in servlet
Listeners and filters in servlet
 
Servlet api & servlet http package
Servlet api & servlet http packageServlet api & servlet http package
Servlet api & servlet http package
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlets
ServletsServlets
Servlets
 
Java servlets
Java servletsJava servlets
Java servlets
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
Servlets
ServletsServlets
Servlets
 
Java EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersJava EE 01-Servlets and Containers
Java EE 01-Servlets and Containers
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
 
J2ee servlet
J2ee servletJ2ee servlet
J2ee servlet
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 

Similar to Servlet in java , java servlet , servlet servlet and CGI, API

Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesbharathiv53
 
Java Servlet
Java ServletJava Servlet
Java ServletYoga Raja
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servletssbd6985
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteTushar B Kute
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technologyMinal Maniar
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletPayal Dungarwal
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxkarthiksmart21
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGPrabu U
 
Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servletsAamir Sohail
 

Similar to Servlet in java , java servlet , servlet servlet and CGI, API (20)

Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
 
Java servlets
Java servletsJava servlets
Java servlets
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Servlet 01
Servlet 01Servlet 01
Servlet 01
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
 
Servlets
ServletsServlets
Servlets
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 
Unit5 servlets
Unit5 servletsUnit5 servlets
Unit5 servlets
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Servlet basics
Servlet basicsServlet basics
Servlet basics
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.Servlet
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
 
gayathri.pptx
gayathri.pptxgayathri.pptx
gayathri.pptx
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMING
 
Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servlets
 
Ecom 1
Ecom 1Ecom 1
Ecom 1
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 

Servlet in java , java servlet , servlet servlet and CGI, API

  • 1.
  • 3. SERVLET:  Servlets provide a component-based, platform- independent method for building Web based applications, without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.  Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
  • 4. Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI.  Performance is significantly better.  Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.  Servlets are platform independent because they are written in Java.  Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.
  • 5. Servlet interface between client and server:
  • 6. CGI(Common Gateway Interface): CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.
  • 8.  Better performance: Because it creates a thread for each request not process.  Portability: Because it uses java language.  Robust: Servlets are managed by JVM so we don't need to worry about memory leak, garbage collection etc.  Secure: Because it uses java language..
  • 9. Website:  Website is a collection of related web pages that may contain text, images, audio and video. The first page of a website is called home page. Each website has specific internet address (URL) that you need to enter in your browser to access a website.  Website is hosted on one or more servers and can be accessed by visiting its homepage using a computer network. A website is managed by its owner that can be an individual, company or an organization.
  • 10. HTTP:  The Hypertext Transfer Protocol (HTTP) is application-level protocol for collaborative, distributed, hypermedia information systems. It is the data communication protocol used to establish communication between client and server.  HTTP is TCP/IP based communication protocol, which is used to deliver the data like image files, query results, HTML files etc on the World Wide Web (WWW) with the default port is TCP 80. It provides the standardized way for computers to communicate with each other.
  • 12. Servlet API:  The javax.servlet and javax.servlet.http packages represent interfaces and classes for servlet API.  The javax.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.  The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.
  • 13. javax.servlet: The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container. javax.servlet.http: The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container.
  • 14. Classes in javax.servlet package: Generic Servlet Servlet Input Stream Servlet Output Stream Servlet Request Wrapper Servlet Response Wrapper Servlet Request Event Servlet Context Event Servlet Request Attribute Event Servlet Context Attribute Event Servlet Exception Unavailable Exception
  • 15. Classes in javax.servlet.http package: Http Servlet Cookie Http Servlet Request Wrapper Http Servlet Response Wrapper Http Session Event Http Session Binding Event Http Utils (deprecated now)
  • 16. Http Servlet class:  The Http Servlet class extends the Generic Servlet class and implements Serializable interface. It provides http specific methods such as doGet , doPost , doHead , doTrace etc.
  • 17. Life Cycle of a Servlet (Servlet Life Cycle):  The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet: Servlet class is loaded. Servlet instance is created. init method is invoked. service method is invoked. destroy method is invoked.
  • 18. Diagram of servlet life cycle:
  • 19.  As displayed in the above diagram, there are three states of a servlet: New Ready End  The servlet is in new state if servlet instance is created. After invoking the init() method, Servlet comes in the ready state. In the ready state, servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state.
  • 20. Written inside the public service method: The public service method converts the Servlet Request object into the Http Servlet Request type and Servlet Response object into the Http Servlet Response type. Then, calls the service method passing these objects.
  • 21. PROGRAM: public void service(Servlet Request req , Servlet Response res) throws Servlet Exception, IOException { Http Servlet Request request; Http Servlet Response response; try { request = (Http Servlet Request)req; response = (Http Servlet Response)res; } catch(Class Cast Exception e) { throw new Servlet Exception("non HTTP request or response"); } service(request, response); }
  • 22. Written inside the protected service method: The protected service method checks the type of request, if request type is get, it calls doGet method, if request type is post, it calls doPost method, so on. Let's see the internal code:
  • 23. PROGRAM: protected void service(HttpServletRequest req, HttpServlet Response resp) throws ServletException, IOException { String method = req . getMethod(); if(method . equals("GET")) { long lastModified = getLastModified(req); if(lastModified == -1L) { } .... } }