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

PPTX
PPTX
Servlets
DOCX
Servlet
PPTX
Core web application development
PPT
PPTX
Chapter 3 servlet & jsp
PPTX
Java Servlets
Servlets
Servlet
Core web application development
Chapter 3 servlet & jsp
Java Servlets

What's hot (20)

PPT
An Introduction To Java Web Technology
PPT
Listeners and filters in servlet
PPTX
Servlet api & servlet http package
PPTX
Servletarchitecture,lifecycle,get,post
PPT
Web Tech Java Servlet Update1
PPT
Knowledge Sharing : Java Servlet
PPT
JAVA Servlets
PPTX
Servlet.ppt
PDF
Servlets
PPT
Java servlets
PPTX
Javax.servlet,http packages
PPTX
PDF
Java EE 01-Servlets and Containers
PPT
Request dispatching in servlet
PPTX
J2ee servlet
PPT
Java Servlet
PPT
Java Servlets
PPT
Java servlet life cycle - methods ppt
PPT
Servlet ppt by vikas jagtap
An Introduction To Java Web Technology
Listeners and filters in servlet
Servlet api & servlet http package
Servletarchitecture,lifecycle,get,post
Web Tech Java Servlet Update1
Knowledge Sharing : Java Servlet
JAVA Servlets
Servlet.ppt
Servlets
Java servlets
Javax.servlet,http packages
Java EE 01-Servlets and Containers
Request dispatching in servlet
J2ee servlet
Java Servlet
Java Servlets
Java servlet life cycle - methods ppt
Servlet ppt by vikas jagtap
Ad

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

PPTX
Http Server Programming in JAVA - Handling http requests and responses
PPTX
Java servlets
PPTX
SERVLET in web technolgy engineering.pptx
PPTX
Java Servlet
PPTX
Unitwwsbdsbsdbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-4.pptx
PPT
Servlet 01
PPTX
java Servlet technology
PPTX
UNIT-3 Servlet
DOCX
TY.BSc.IT Java QB U3
PPTX
PDF
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
PPTX
Wt unit 3
DOC
Unit5 servlets
PDF
Java servlet technology
DOC
Servlet basics
PDF
Servlet classnotes
PPTX
SERVLETS (2).pptxintroduction to servlet with all servlets
PPTX
Advance Java Programming (CM5I) 6.Servlet
PPTX
WEB TECHNOLOGY Unit-3.pptx
PPTX
gayathri.pptx
Http Server Programming in JAVA - Handling http requests and responses
Java servlets
SERVLET in web technolgy engineering.pptx
Java Servlet
Unitwwsbdsbsdbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-4.pptx
Servlet 01
java Servlet technology
UNIT-3 Servlet
TY.BSc.IT Java QB U3
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Wt unit 3
Unit5 servlets
Java servlet technology
Servlet basics
Servlet classnotes
SERVLETS (2).pptxintroduction to servlet with all servlets
Advance Java Programming (CM5I) 6.Servlet
WEB TECHNOLOGY Unit-3.pptx
gayathri.pptx
Ad

Recently uploaded (20)

PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
From loneliness to social connection charting
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
Congenital Hypothyroidism pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Insiders guide to clinical Medicine.pdf
PDF
High Ground Student Revision Booklet Preview
Open Quiz Monsoon Mind Game Prelims.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
human mycosis Human fungal infections are called human mycosis..pptx
From loneliness to social connection charting
Onica Farming 24rsclub profitable farm business
Cardiovascular Pharmacology for pharmacy students.pptx
Introduction and Scope of Bichemistry.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
The Final Stretch: How to Release a Game and Not Die in the Process.
How to Manage Starshipit in Odoo 18 - Odoo Slides
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Congenital Hypothyroidism pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Open Quiz Monsoon Mind Game Final Set.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Insiders guide to clinical Medicine.pdf
High Ground Student Revision Booklet Preview

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