[go: up one dir, main page]

0% found this document useful (0 votes)
9 views13 pages

Unit II SERVLET

Servlet technology is used for creating web applications, offering advantages over CGI such as better performance, portability, and security. A servlet is a web component that responds to client requests and is managed by the Java Virtual Machine (JVM). The servlet architecture includes key methods like init(), service(), and destroy() for handling requests and managing the servlet lifecycle.

Uploaded by

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

Unit II SERVLET

Servlet technology is used for creating web applications, offering advantages over CGI such as better performance, portability, and security. A servlet is a web component that responds to client requests and is managed by the Java Virtual Machine (JVM). The servlet architecture includes key methods like init(), service(), and destroy() for handling requests and managing the servlet lifecycle.

Uploaded by

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

Unit II

SERVERSIDE SCRIPTING
Servlet
 Servlet technology is used to create web application
 Before servlet, CGI (Common Gateway Interface) scripting language
was popular as a server-side programming language.
 There are many interfaces and classes in the servlet API such as
Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse
etc.
 Servlet is an interface that must be implemented for creating any
servlet.
 Servlet is a class that extend the capabilities of the servers and
respond to the incoming request. It can respond to any type of
requests.
 Servlet is a web component that is deployed on the server to create
dynamic web page.
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 components such as HTML. The web components typically
execute in Web Server and respond to 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:

1. If number of client’s increases, it takes more time for


sending response.

2. For each request, it starts a process and Web server is


limited to start processes.

3. It uses platform dependent language e.g. C, C++, Perl.


Advantage of Servlet
 There are many advantages of servlet over CGI. The web container creates threads for
handling the multiple requests to the servlet. Threads have a lot of benefits over the Processes
such as they share a common memory area, lightweight, cost of communication between the
threads are low.

 The basic benefits of servlet are as follows:

1. Better Performance: because it creates a thread for each request not process.

2. Portability: because it uses java language.

3. Robust: Servlets are managed by JVM so no need to worry about memory leak, garbage
collection etc.

4. Secure: because it uses java language.


Architecture of Servelet
 A Servlet is a class, which implements the javax.servlet.Servlet interface.
However instead of directly implementing the javax.servlet.Servlet interface
 we extend a class that has implemented the interface like
javax.servlet.GenericServletor javax.servlet.http.HttpServlet.
Servlet Execution

 This is the process of a servlet execution takes place when client (browser)
makes a request to the webserver.
Servlet architecture includes:
a) Servlet Interface
To write a servlet we need to implement Servlet interface. Servlet interface can be implemented directly
or indirectly by extending GenericServlet or HttpServlet class.

b) Request handling methods


There are 3 methods defined in Servlet interface: init(), service() and destroy().

The first time a servlet is invoked, the init method is called. It is called only once during the lifetime of
a servlet.

The Service method is used for handling the client request. As the client request reaches to the
container it creates a thread of the servlet object, and request and response object are also created.
These request and response object are then passed as parameter to the service method, which then
process the client request. The service method in turn calls the doGet or doPost methods (if the user
has extended the class from HttpServlet).
c) Number of instances
Basic Structure of a Servlet
Public class firstServlet extends HttpServlet
{
publicvoidinit()
{
/* Put your initialization code in this method,
* as this method is called only once */
}
publicvoid service()
{ // Service request for Servlet }
publicvoid destroy()
{
// For taking the servlet out of service, this method is
called only once }}

You might also like