Introduction of Servlet
Servlet is a simple java program that runs on server and capable of handling request and generating
dynamic response for the client.
1) Servlet is a technology which is used to create a web application.
2) Servlet is an API that provides many interfaces and classes including documentation.
3) Servlet is an interface that must be implemented for creating any Servlet.
4) Servlet is a class that extends the capabilities of the servers and responds to the incoming
requests. It can respond to any requests.
5) Servlet is a web component that is deployed on the server to create a dynamic web page.
What is a web application?
A web application is an application accessible from the web. A web application is composed of web
components like Servlet, JSP, Filter, etc. and other elements such as HTML, CSS, and JavaScript. The
web components typically execute in Web Server and respond to the HTTP request.
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.
Disadvantages of CGI
There are many problems in CGI technology:
If the number of clients increases, it takes more time for sending the response.
For each request, it starts a process, and the web server is limited to start processes.
It uses platform dependent language e.g. C, C++, Perl.
DB
www.technid Server
request
hi.com
Servlet
Chrome
firefox
Response
html Java program
We have to import javax.servlet.*; package
First of all servlet program will execute on server, so we have to install a server either web server or
application server.
There are number of servers available in market to execute servlet.
We will install Tomcat server...
There are two ways to create a servlet in java
1) Creating Servlet Using javax.servlet.Servlet Interface
2) Creating Servlet Using GenericServlet abstract class
How to create servlet using javax.servlet.Servlet Interface.
There are five methods in Servlet interface
1) Public abstract void init(javax.servlet.ServletConfig)
2) Public ServletConfiq getServletConfig();
3) Public void Service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
4) Public abstract java.lang.String getServiceInfo();
5) Public abstract void destroy();
Three methods are servlet life cycle methods and 2 are non-life cycle methods
All five methods has to be overridden by the user defined class or implementing class.
Overview of how to execute a servlet?
User definedclass
Class MyServlet implements
Servlet
{ SERVER
Mapping servlet in xml file for To execute
the server userdefined servlet
Override all the methods here class with the help of
With the help of url pattern xml file
} Deployment descriptor
Creating Servlet Using GenericServlet:
It is an abstract class.
There are 5 methods in this class too but 4 methods has defined body in this class and we have to
define only one method in user defined class or child class.
User definedclass
Class MyServlet extends
GenricServlet
SERVER
{
To execute user
defined servlet class
with the help of xml
Override 1 methods here file
Mapping servlet in xml file for
Service() the server
With the help of URL pattern
{
Deployment descriptor
}