SlideShare a Scribd company logo
U18CSI6201
INTERNET & WEB PROGRAMMING
SERVLETS
V.SENTHIL KUMAR
Department of CSE
COURSE
OUTCOMES
CO1: DESIGN A WEBSITE USING HTML (K5, S3)
CO2: Apply Cascading Style Sheet to design a HTML
Webpage (K3, S2)
CO3: Develop a HTML form and validate it using Java
Script (K5, S2)
CO4: Develop web application using JSP, Servlet (K5,
S3)
CO5: Develop an XML document and validate it using
SCHEMA (K5, S2)
AGENDA
WHAT IS
SERVLET?
ADVANTAGES SERVLET
ARCHITECTURE
SERVLET TASKS SERVLET
PACKAGE
SERVLET LIFE
CYCLE
What are
Servlets?
• Java Servlets are programs that
run on a Web or Application
server.
• 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.
• Implemented using the Common
Gateway Interface (CGI).
Servlet
CGI
(Common
Gateway
Interface)
Disadvantages of
CGI
• There are many problems in CGI technology:
1.If the number of clients increases, it takes
more time for sending the response.
2.For each request, it starts a process, and the
web server is limited to start processes.
3.It uses platform dependent language
e.g. C, C++, perl.
Advantages
of Servlet
Advantages
of servlet
Better performance: because it creates
a thread for each request, not process.
Portability: because it uses Java
language.
Robust: JVM manages Servlets, so we
don't need to worry about the memory
leak, garbage collection, etc.
Secure: because it uses java language.
Servlets
offer several
advantages
• Performance is significantly better.
• Servlets execute within the address
space of a Web server.
• Servlets are platform-independent
• Java security manager on the server
enforces a set of restrictions to protect
the resources on a server machine.
• The full functionality of the Java class
libraries is available to a servlet.
Servlet.pptx
Servlets
Architecture
Servlet Container
• It provides the runtime environment for JavaEE
(j2ee) applications.
• The client/user can request only a static
WebPages from the server. If the user wants to
read the web pages as per input then the
servlet container is used in java.
Servlet
Container
Servlet
Container
States
Standalone: It is typical Java-based servers in which the
servlet container and the web servers are the integral
part of a single program. For example:- Tomcat running
by itself
In-process: It is separated from the web server, because
a different program runs within the address space of the
main server as a plug-in. For example:- Tomcat running
inside the JBoss.
Out-of-process: The web server and servlet container
are different programs which are run in a different
process. For performing the communications between
them, web server uses the plug-in provided by the
servlet container.
Server: Web vs.
Application
• Server is a device or a computer program that
accepts and responds to the request made by
other program, known as client. It is used to
manage the network resources and for running
the program or software that provides services.
• There are two types of servers:
1.Web Server
2.Application Server
Web
Server
Web server contains only web or servlet container. It can be used for
servlet, jsp, struts, jsf etc. It can't be used for EJB.
It is a computer where the web content can be stored. In general
web server can be used to host the web sites but there also used
some other web servers also such as FTP, email, storage, gaming etc.
Examples of Web Servers are: Apache Tomcat and Resin.
Generating response by using the script and communicating with
database.
Sending file to the client associated with the requested URL.
Web Server
Application
Server
• Application server contains Web and EJB containers.
It can be used for servlet, jsp, struts, jsf, ejb etc. It is a
component based product that lies in the middle-tier of
a server centric architecture.
• It provides the middleware services for state
maintenance and security, along with persistence and
data access. It is a type of server designed to install,
operate and host associated services and applications
for the IT services, end users and organizations.
Application
Server
Servlets Tasks
Read the explicit
data sent by the
clients (browsers).
1
Read the implicit
HTTP request data
sent by the clients
(browsers).
2
Process the data
and generate the
results.
3
Send the explicit
data (i.e., the
document) to the
clients (browsers).
4
Send the implicit
HTTP response to
the clients
(browsers).
5
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.
Interfaces in
javax.servlet
package
1.Servlet
2.ServletRequest
3.ServletResponse
4.RequestDispatcher
5.ServletConfig
6.ServletContext
7.SingleThreadModel
8.Filter
9.FilterConfig
10.FilterChain
11.ServletRequestListener
12.ServletRequestAttributeListener
13.ServletContextListener
14.ServletContextAttributeListener
Classes in
javax.servle
t package
1.GenericServlet
2.ServletInputStream
3.ServletOutputStream
4.ServletRequestWrapper
5.ServletResponseWrapper
6.ServletRequestEvent
7.ServletContextEvent
8.ServletRequestAttributeEvent
9.ServletContextAttributeEvent
10.ServletException
11.UnavailableException
Interfaces in
javax.servlet.http
package
1.HttpServletRequest
2.HttpServletResponse
3.HttpSession
4.HttpSessionListener
5.HttpSessionAttributeListener
6.HttpSessionBindingListener
7.HttpSessionActivationListener
8.HttpSessionContext
Classes in
javax.servlet.http
package
1.HttpServlet
2.Cookie
3.HttpServletRequestWrapper
4.HttpServletResponseWrapper
5.HttpSessionEvent
6.HttpSessionBindingEvent
7.HttpUtils
Servlets -
Life Cycle
A servlet life cycle can be defined as the entire process from its
creation till the destruction. The following are the paths followed
by a servlet.
The servlet is initialized by calling the init() method.
The servlet calls service() method to process a client's request.
The servlet is terminated by calling the destroy() method.
Finally, servlet is garbage collected by the garbage collector of
the JVM.
The init() Method
• The init method is called only once. It is
called only when the servlet is created,
and not called for any user requests
afterwards. So, it is used for one-time
initializations, just as with the init method
of applets.
The service()
Method
• The service() method is the main method to perform
the actual task. The servlet container (i.e. web server)
calls the service() method to handle requests coming
from the client( browsers) and to write the formatted
response back to the client.
• The service() method checks the HTTP request type
(GET, POST, PUT, DELETE, etc.) and calls doGet,
doPost, doPut, doDelete, etc. methods as
appropriate.
Servlet.pptx
doGet() &
doPost()
The destroy()
Method
• 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.
Architecture
Diagram
Architecture
First the HTTP requests
coming to the server are
delegated to the servlet
container.
1
The servlet container loads
the servlet before invoking
the service() method.
2
Then the servlet container
handles multiple requests
by spawning multiple
threads, each thread
executing the service()
method of a single instance
of the servlet.
3

More Related Content

Servlet.pptx