What is a Servlet?
o Servlet is a technology which is used to create a web application.
o Servlet is an API that provides many interfaces and classes including
documentation.
o Servlet is an interface that must be implemented for creating any Servlet.
o Servlet is a class that extends the capabilities of the servers and responds to the
incoming requests. It can respond to any requests.
o Servlet is a web component that is deployed on the server to create a dynamic
web page.
Java servlets are server-side programs (running inside a web server) that handle clients' requests and
return a customized or dynamic response for each request. The dynamic response could be based on
user's input (e.g., search, online shopping, online transaction) with data retrieved from databases or
other applications, or time-sensitive data (such as news and stock prices).
Java servlets typically run on the HTTP protocol. HTTP is an asymmetrical request-response protocol.
The client sends a request message to the server, and the server returns a response message as
illustrated.
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.
By : Trupti Rongare
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 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
By : Trupti Rongare
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:
1. Better performance: because it creates a thread for each request, not process.
2. Portability: because it uses Java language.
3. Robust: JVM manages Servlets, so we don't need to worry about the memory
leak, garbage collection, etc.
4. Secure: because it uses java language.
What are website.
Static Website
Dynamic Website
Static website
Static website is the basic type of website that is easy to create. You don't need the
knowledge of web programming and database design to create a static website. Its web
pages are coded in HTML.
The codes are fixed for each page so the information contained in the page does not
change and it looks like a printed page.
Dynamic website
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.
By : Trupti Rongare
Client side scripting generates content at the client computer on the basis of user input.
The web browser downloads the web page from the server and processes the code
within the page to render information to the user.
In server side scripting, the software runs on the server and processing is completed in
the server then plain pages are sent to the user.
HTTP Requests
The request sent by the computer to a web server, contains all sorts of potentially
interesting information; it is known as HTTP requests.
The HTTP client sends the request to the server in the form of request message which
includes following information:
o The Request-line
o The analysis of source IP address, proxy and port
o The analysis of destination IP address, protocol, port and host
o The Requested URI (Uniform Resource Identifier)
o The Request method and Content
o The User-Agent header
o The Connection control header
o The Cache control header
The HTTP request method indicates the method to be performed on the resource
identified by the Requested URI (Uniform Resource Identifier). This method is
case-sensitive and should be used in uppercase.
The HTTP request methods are:
By : Trupti Rongare
HTTP Description
Request
GET Asks to get the resource at the requested URL.
POST Asks the server to accept the body info attached. It is like GET request
with extra info sent with the request.
HEAD Asks for only the header part of whatever a GET would return. Just
like GET but with no body.
TRACE Asks for the loopback of the request message, for testing or
troubleshooting.
PUT Says to put the enclosed info (the body) at the requested URL.
DELETE Says to delete the resource at the requested URL.
OPTIONS Asks for a list of the HTTP methods to which the thing at the request
URL can respond
Servlet API
The servlet API is included into two packages: javax.servlet and javax.servlet.http.
The first package is intended to be generic and the second specific to the HTTP and HTTPS protocols.
Servlet are java classes that extend javax.servlet.http.HttpServlet. servlet perform their role in web
apps by calling the servlet API.
Javax.servlet package is the core of servlet API.
This package includes classes for:
● Communicating with the host server and client:
ServletRequest
ServletResponse
● Communicating with the client:
ServletInputStream
ServletOutputStream
By : Trupti Rongare
The Servlet interface defines methods to initialise a servlet, to receive and respond to client requests,
and to destroy a servlet and its resources. These methods are called by the network service in the
following manner:
1. Servlet is created then initialised.
2. Zero or more service calls from clients are handled
3. Servlet is destroyed then garbage collected and finalised
The following are the methods available in this interface:
Methods Description
init() Initializes the servlet. The method is called once,
automatically, by the servlet engine when it
loads the servlet. It is guaranteed to finish before
any service requests are accepted.
The init method should save the ServletConfig
object so that it can be returned by the
getServletConfig method. If a fatal initialization
error occurs, the init method should throw an
appropriate "UnavailableException" exception.
getServletConfig() Returns a servlet config object, which contains
any initialization parameters and startup
configuration for this servlet. This is the
ServletConfig object passed to the init method;
the init method should have stored this object so
that this method could return it.
Service() Carries out a single request from the client. The
method implements a request and response
paradigm. The request object contains
information about the service request, including
parameters provided by the client. The response
object is used to return information to the client.
The request and response objects rely on the
underlying network transport for quality of
service guarantees, such as reordering,
duplication, privacy, and authentication.
Service requests are not handled until servlet
initialization has completed. Any requests for
service that are received during initialization
block until it is complete. Note that servlets
typically run inside multi-threaded servers;
servers can handle multiple service requests
simultaneously. It is the servlet writer's
responsibility to synchronize access to any
shared resources, such as network connections
or the servlet's class and instance variables. `
By : Trupti Rongare
getServletInfo() Returns a string containing information about
the servlet, such as its author, version, and
copyright. As this method may be called to
display such information in an administrative
tool that is servlet engine specfic, the string that
this method returns should be plain text and not
contain markup.
destroy() Cleans up whatever resources are being held
(e.g., memory, file handles, threads) and makes
sure that any persistent state is synchronized
with the servlet's current in-memory state. The
method is called once, automatically, by the
network service when it unloads the servlet.
After destroy is run, it cannot be called again
until the network service reloads the servlet.
When the network service removes a servlet, it
calls destroy after all service calls have been
completed, or a service-specific number of
seconds have passed, whichever comes first. In
the case of long-running operations, there could
be other threads running service requests when
destroy is called.
The servlet writer is responsible for making sure
that any threads still in the service method
complete.
Servlet Interface
1. Servlet Interface
2. Methods of Servlet interface
Servlet interface provides common behavior 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
There are 5 methods in Servlet interface. The init, service and destroy are the life cycle
methods of servlet. These are invoked by the web container.
By : Trupti Rongare
Method Description
public void init(ServletConfig config) initializes the servlet. It is the life cycle
method of servlet and invoked by the
web container only once.
public void service(ServletRequest provides response for the incoming
request,ServletResponse response) request. It is invoked at each request
by the web container.
public void destroy() is invoked only once and indicates that
servlet is being destroyed.
public ServletConfig returns the object of ServletConfig.
getServletConfig()
public String getServletInfo() returns information about servlet such
as writer, copyright, version etc.
Servlet Example by implementing Servlet interface
Let's see the simple example of servlet by implementing the servlet interface.
1. import java.io.*;
2. import javax.servlet.*;
3.
4. public class First implements Servlet{
5. ServletConfig config=null;
6.
7. public void init(ServletConfig config){
8. this.config=config;
9. System.out.println("servlet is initialized");
10.}
11.
12.public void service(ServletRequest req,ServletResponse res)
13.throws IOException,ServletException{
14.
15.res.setContentType("text/html");
16.
17.PrintWriter out=res.getWriter();
18.out.print("<html><body>");
19.out.print("<b>hello simple servlet</b>");
By : Trupti Rongare
20.out.print("</body></html>");
21.
22.}
23.public void destroy(){System.out.println("servlet is destroyed");}
24.public ServletConfig getServletConfig(){return config;}
25.public String getServletInfo(){return "copyright 2007-1010";}
26.
27.}
GenericServlet class
1. GenericServlet class
2. Methods of GenericServlet class
3. Example of GenericServlet class
GenericServlet class implements Servlet, ServletConfig and Serializable interfaces.
It provides the implementation of all the methods of these interfaces except the service
method.
GenericServlet class can handle any type of request so it is protocol-independent.
You may create a generic servlet by inheriting the GenericServlet class and providing the
implementation of the service method.
Servlet Example by inheriting the GenericServlet class
1. import java.io.*;
2. import javax.servlet.*;
3.
4. public class First extends GenericServlet{
5. public void service(ServletRequest req,ServletResponse res)
6. throws IOException,ServletException{
7.
8. res.setContentType("text/html");
9.
10.PrintWriter out=res.getWriter();
11.out.print("<html><body>");
12.out.print("<b>hello generic servlet</b>");
13.out.print("</body></html>");
14.
15.}
16.}
By : Trupti Rongare
By : Trupti Rongare