0% found this document useful (0 votes)
36 views

Chapter-7 (Servlets)

The document discusses servlets, including what they are, their advantages over CGI, the servlet lifecycle, and servlet containers. Servlets are Java programs that run on the server-side to generate dynamic web pages. They provide better performance than CGI since they are multithreaded and share memory.

Uploaded by

Nasis Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Chapter-7 (Servlets)

The document discusses servlets, including what they are, their advantages over CGI, the servlet lifecycle, and servlet containers. Servlets are Java programs that run on the server-side to generate dynamic web pages. They provide better performance than CGI since they are multithreaded and share memory.

Uploaded by

Nasis Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter-7 (Servlets)

By: Joseph W.

1
Outline
Introduction
Servlet Interface
GenericServlet and HttpServlet
Life cycle of a servlet
How servlet works

2/7/2023 2
Introduction
Servlet technology is used to create a web application (resides at server side and generates a
dynamic web page).
Servlet technology is robust and scalable because of java language.
A web application is an application accessible from the web.
A web application is composed of web components like Servlet, JSP etc. and other elements such
as HTML, CSS, and JavaScript.
The web components typically execute in Web Server and respond to the HTTP request.
Before Servlet, CGI (Common Gateway Interface) scripting language was common as a server-side
programming language. However, there were many disadvantages to this technology.
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.

2/7/2023 3
Cont.
Disadvantages of CGI
o If the number of clients increases, it takes more time for sending the response.
o For each request, it starts a process, and the web server is limited to start processes.
o It uses platform dependent language.
Advantages of Servlets
There are many advantages of Servlet over CGI.
The web container creates threads for handling the multiple requests to the Servlet.
Threads have many benefits over the Processes such as they share a common memory area,
lightweight, cost of communication between the threads are low.
The advantages of Servlet are as follows:

2/7/2023 4
Cont.
o Better performance: because it creates a thread for each request, not process.
o Portability: because it uses Java language.
o Robust: JVM manages Servlets, so we don't need to worry about the memory leak,
garbage collection , etc.
o Secure: because it uses java language.
Terminologies:
Website:
o Website is a collection of related web pages that may contain text, images, audio and video.
o The first page of a website is called home page.
o Each website has specific internet address (URL) that you need to enter in your browser to
access a website.

2/7/2023 5
Cont.
o A website can be of two types: Static Website and Dynamic Website
o Static Website:
✓ The codes are fixed for each page so the information contained in the page does not
change and it looks like a printed page.
✓ Its web pages are coded in HTML.
o Dynamic website:
✓ is a collection of dynamic web pages whose content changes dynamically.
✓ It accesses content from a database or Content Management System (CMS).
✓ Therefore, when you alter or update the content of the database, the content of the
website is also altered or updated.
✓ Dynamic website uses client-side scripting or server-side scripting, or both to generate
dynamic content.

2/7/2023 6
Cont.
HTTP:
o The Hypertext Transfer Protocol (HTTP) is application-level protocol for collaborative,
distributed, hypermedia information systems.
o It is the data communication protocol used to establish communication between client and
server.
o 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.
o It provides the standardized way for computers to communicate with each other.
o There are three fundamental features that make the HTTP a simple and powerful protocol
used for communication:
1) HTTP is media independent: It specifies that any type of media content can be sent by
HTTP as long as both the server and the client can handle the data content.

2/7/2023 7
Cont.
2) HTTP is connectionless: It is a connectionless approach in which HTTP client i.e., a browser
initiates the HTTP request and after the request is sent the client disconnects from server
and waits for the response.
3) HTTP is stateless: The client and server are aware of each other during a current request
only. Afterwards, both of them forget each other. Due to the stateless nature of protocol,
neither the client nor the server can retain the information about different request across the
web pages.
HTTP Requests
o The request sent by the computer to a web server, contains all sorts of potentially
interesting information; it is known as HTTP requests.
o The HTTP request method indicates the method to be performed on the resource identified
by the Requested URI (Uniform Resource Identifier).
o This method is case-sensitive and should be used in uppercase.

2/7/2023 8
Cont.
o The HTTP request methods are:

2/7/2023 9
Cont.
Servlet Containers:
o It provides the runtime environment for JavaEE (j2ee) applications.
o 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.
o The servlet container is the part of web server which can be run in a separate process.
o We can classify the servlet container states in three types:
1) 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
2) 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.

2/7/2023 10
Cont.
3) 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.
o Some of the benefits of servlet containers: Life Cycle Management, Multithreaded support
Object pooling, Security, e.t.c.
Content Type
o Content Type is also known as MIME (Multipurpose Internet Mail Extension) Type.
o It is a HTTP header that provides the description about what are you sending to the
browser.
o MIME is an internet standard that is used for extending the limited capabilities of email by
allowing the insertion of sounds, images and text in a message.
o It supports the unlimited message length.

2/7/2023 11
Cont.
o Examples of content type: text/html, text/plain, application/json, application/x-zip,
images/jpeg, images/png, images/gif, audio/mp3, video/mp4
Servlet API:
o The javax.servlet and javax.servlet.http packages represent interfaces and classes for
servlet api.
o 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.
o The javax.servlet.http package contains interfaces and classes that are responsible for
http requests only.

2/7/2023 12
Servlet Interface
Servlet interface provides common behaviour to all the servlets.
Servlet interface defines methods that all servlets must implement.
Servlet interface needs to be implemented for creating any servlet (either directly or indirectly).
It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to
destroy the servlet and 2 non-life cycle methods.
Methods of Servlet interface
o There are 5 methods in Servlet interface.
o The init, service and destroy are the life cycle methods of servlet. These are invoked by
the web container.

2/7/2023 13
Cont.

2/7/2023 14
GenericServlet class & HttpServlet class
GenericServlet
o GenericServlet class implements Servlet, ServletConfig and Serializable interfaces.
o It provides the implementation of all the methods of these interfaces except the
service method.
o GenericServlet class can handle any type of request so it is protocol-independent.
o You may create a generic servlet by inheriting the GenericServlet class and providing the
implementation of the service method.
HttpServlet
o The HttpServlet class extends the GenericServlet class and implements Serializable
interface.
o It provides http specific methods such as doGet, doPost, doPut, doDelete, e.t.c.

2/7/2023 15
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:
1) Servlet class is loaded
o The class-loader is responsible to load the servlet class.
o The servlet class is loaded when the first request for the servlet is received by the web
container.
2) Servlet instance is created
o The web container creates the instance of a servlet after loading the servlet class.
o The servlet instance is created only once in the servlet life cycle.
3) init method is invoked
o The web container calls the init method only once after creating the servlet instance.

2/7/2023 16
Cont.
o The init method is used to initialize the servlet.
o It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init
method is given below:
public void init(ServletConfig config) throws ServletException

4) service method is invoked


o The web container calls the service method each time when request for the servlet is
received.
o If servlet is not initialized, it follows the first three steps as described above then calls
the service method. If servlet is initialized, it calls the service method.
o Notice that servlet is initialized only once. The syntax of the service method of the
Servlet interface is given below:
public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException

2/7/2023 17
Cont.
5) destroy method is invoked
o The web container calls the destroy method before removing the servlet instance from
the service.
o It gives the servlet an opportunity to clean up any resource for example memory, thread
etc.
o The syntax of the destroy method of the Servlet interface is given below:
public void destroy()

2/7/2023 18
How Servlet Works
The server checks if the servlet is requested for the first time.
If yes, web container does the following tasks:
o loads the servlet class.
o instantiates the servlet class.
o calls the init method passing the ServletConfig object
Else
o calls the service method passing request and response objects
The web container calls the destroy method when it needs to remove the servlet such as at time of
stopping server or undeploying the project.

2/7/2023 19
Thank You!!

20

You might also like