SlideShare a Scribd company logo
Servlet in java , java servlet , servlet servlet and CGI, API
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 (20)

An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
vikram singh
 
Listeners and filters in servlet
Listeners and filters in servletListeners and filters in servlet
Listeners and filters in servlet
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Servlet api & servlet http package
Servlet api & servlet http packageServlet api & servlet http package
Servlet api & servlet http package
renukarenuka9
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
vamsi krishna
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
vikram singh
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
Fahmi Jafar
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
deepak kumar
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
VMahesh5
 
Servlets
ServletsServlets
Servlets
Ravi Kant Sahu
 
Java servlets
Java servletsJava servlets
Java servlets
lopjuan
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
vamsi krishna
 
Servlets
ServletsServlets
Servlets
Rajkiran Mummadi
 
Java EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersJava EE 01-Servlets and Containers
Java EE 01-Servlets and Containers
Fernando Gil
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
vikram singh
 
J2ee servlet
J2ee servletJ2ee servlet
J2ee servlet
vinoth ponnurangam
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
Rajiv Gupta
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
Nitin Pai
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
kamal kotecha
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
Vikas Jagtap
 

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

UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
ssbd6985
 
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
Servlets-UNIT3and introduction to servlet
Servlets-UNIT3and introduction to servletServlets-UNIT3and introduction to servlet
Servlets-UNIT3and introduction to servlet
RadhikaP41
 
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
Module 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.ppt
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
Servlet (1) also contains code to create it.ppt
Servlet (1) also contains code to create it.pptServlet (1) also contains code to create it.ppt
Servlet (1) also contains code to create it.ppt
juhishrivastava25
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
MouDhara1
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
kstalin2
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
KhushalChoudhary14
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
karthiksmart21
 
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgbServlet123jkhuiyhkjkljioyudfrtsdrestfhgb
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
shubhangimalas1
 
SERVLET in web technolgy engineering.pptx
SERVLET in web technolgy engineering.pptxSERVLET in web technolgy engineering.pptx
SERVLET in web technolgy engineering.pptx
ARUNKUMARM230658
 
servlets sessions and cookies, jdbc connectivity
servlets sessions and cookies, jdbc connectivityservlets sessions and cookies, jdbc connectivity
servlets sessions and cookies, jdbc connectivity
snehalatha790700
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMING
Prabu U
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
ramya marichamy
 
Java servlets
Java servletsJava servlets
Java servlets
yuvarani p
 
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
VENKATESHBHAT25
 
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
bharathiv53
 
Servlets
ServletsServlets
Servlets
Akshay Ballarpure
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
team11vgnt
 
Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servlets
Aamir Sohail
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
ssbd6985
 
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
Servlets-UNIT3and introduction to servlet
Servlets-UNIT3and introduction to servletServlets-UNIT3and introduction to servlet
Servlets-UNIT3and introduction to servlet
RadhikaP41
 
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
Module 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.pptModule 4.ppt
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
Servlet (1) also contains code to create it.ppt
Servlet (1) also contains code to create it.pptServlet (1) also contains code to create it.ppt
Servlet (1) also contains code to create it.ppt
juhishrivastava25
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
kstalin2
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
karthiksmart21
 
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgbServlet123jkhuiyhkjkljioyudfrtsdrestfhgb
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
shubhangimalas1
 
SERVLET in web technolgy engineering.pptx
SERVLET in web technolgy engineering.pptxSERVLET in web technolgy engineering.pptx
SERVLET in web technolgy engineering.pptx
ARUNKUMARM230658
 
servlets sessions and cookies, jdbc connectivity
servlets sessions and cookies, jdbc connectivityservlets sessions and cookies, jdbc connectivity
servlets sessions and cookies, jdbc connectivity
snehalatha790700
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMING
Prabu U
 
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
VENKATESHBHAT25
 
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
bharathiv53
 
Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servlets
Aamir Sohail
 
Ad

Recently uploaded (20)

How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Ad

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

  • 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) { } .... } }