R4R provide basic Servlet Tutorials concept with
Servlet Examples .
Through R4R you can develop
Servlet programming concept. R4R provide
Servlet Interview Questions with answers.R4R provide
Servlet Languages study materials in easy way.
2.1 Servlet Basic Example
Servlet Hot Topics
4.1 Servlet Basic
JAVA SERVLET TECHNOLOGY
A Servlet is a Java programming language class used to extend
the capabilities of servers that host applications accessed via a
request-response programming model. Although servlets can respond to any type
of request, they are commonly used to extend the applications hosted by
servers. For such applications,
Java Servlet technology defines HTTP-specific servlet classes. The javax.servlet and
javax.http.servlet are two
packages which provided
interface and classes for writing servlets. All the servlet must implement servlet interface ,which defines
life cycle method
When implementing a generic service, you can use or extend the
GenericServlet class provided with the Java Servlet API. The
provides methods, such as doGet and
doPost, for handling HTTP-specific services.
HTTP Servlet typically used to:
Provide the dynamically contents which get the result from the server.
Process are used to store the data which is submitted by the HTML.
Manage information about the state of a stateless HTTP. e.g. an online
shopping car manages request for multiple concurrent customers.
GENERIC SERVLET CLASS CONTAINS FIVE
The generic servlet is the base class of servlet which contains
1.init() method:- Init()
called only once by the servlet container in its whole lofecycle the life of a servlet. The init() method takes a ServletConfig object that contains the
initialization parameters and servlet's configuration and throws a
ServletException if an exception has occurred.
2.Service() method :- This method
defined as public void service as (ServletRequest
req, ServletResponse res) which throws ServletException, as IOException
.If once the servlet starting getting the requests, the service()
method is called by the servlet container to respond.
3.getservlet config():- This method is defined as public ServletConfig
getServletConfig() this method contain initialization and startup of the servlet
and returns the Servlet Config object
4.getservlet info():- This
method is defined as public String getServletInfo()
The information about the servlet is returned by this method like version,
author etc. This method returns a string which should be in the form of plain text and not any kind of markup.
method is defined as public destroy().this method is called when want to close the servlet.
LIFE CYCLE OF SERVLET
The life cycle of a servlet is controlled by the container in
which the servlet has been deployed. The life cycle of a servlet can be
categorized into four parts:
Loading and instantiation:-The servlet container
loads the servlet during startup or when the first request is made .After
loading of servlet container create the instance of the servlet
Initialization:-When creating the instances,after that
the servlet container calls the init() method .then
passes the servlet initialization parameters to the init()
method. init() method is called once through out the life cycle of servlet
Servicing the request :-After successfully
initialization ,servlet container call the service method for servising
any request. The service() method determines the kind of request and calls the appropriate method
(doGet() or doPost()) for handling the request and sends response to the
client using the methods of the response object
The servlet Destroying :-If the servlet is no
longer required for servicing any request, the servlet container will calls the
destroy() method .
ADVANTAGES OF JAVA SERVLET
Java servlet provide various advantages to build a server side
programming. Following are the advantages of java
Portable:- As servlets are written in java and
follow APIs of java so they are very high portable over operating
systems and serve implementations.
Efficient:- As compared to CGI applications
servlet is more efficient
Extensibility:-The servlet API is designed in such a way that it can
be easily extensible. As for now the servlet API
support Http Servlets, but in later it can be extended for another type of
Safety:- as java is safe servlets are also safe
,garbage collection prevent the leakage of memory and exception handling throws
the exceptions for the errors.
BASIC SERVLET STRUCTURE
public class Servletname extends HttpServlet
public void doGet (HttpRequest request,HttpResponse response) throws ServletException, IOException
//code for business logic
//use request object to read client request
//user response object to throw output back to the client
}//close do get
Servlet life cycle include mainly four steps init()--->service()--->doGet() or doPost()--->destroy
A SERVLET PROGRAM (using its life
//servlet program using its life-cycle
public class Testservlet extends HttpServlet
public void init() trows ServletException
i=0; //initializing i value
//incrementing i value in doGet method
public void doGet(HttpServletRequest request, HttpServletReponse response)
throws IOEXception ,ServletException
PrintWriter out= resonse.getWriter():
out.println ("<title> Hello world </title>");
out.println ("<h1> value of i is initialized in init method </h1>"+"<h1>" + i + "</h1>"
i = i+;
if ((i == 10)
out.println ("<title> HEllo world</title>");
out.println ("<h1> i's value reaches 10 hence calling destroy method to reset it </h1>' + "<h1>" + "</h1>");
destroy(); //call destroy method if i=10
if (i<10) //display increment value of i
out.prinln("<title> Hello world </title>");
out.prinln("<h1> value of i incremented in doGet </h1>" + "<h1>" + i + "<h1>");
public void destroy()//reset i value here
To run servlet first install and configure the server on your
system. web server is required to install to run the servlet.
To compileAnd Run:
Servlets can be called directly by typing their uniform resource locator
(URL) into a browser's location window after you've started the server.
Servlets can also be invoked from an HTML form by specifying their URL in the
definition for a Submit button, for example.
Servlets can be called by any program that can open an hypertext transfer
protocol (HTTP) request.
What is web.xml file?
Web. xml file also called "web Deployment descriptor" allows us
to configure our web application inside the Servlet containerHere we can specify the name of our web application, define our
Java Servlets, specify initialization parameters for Servlets, define tag
libraries, and a whole lot more. This file is placed inside the /WEB-INF folder. we use two of the features of 'web.xml' file; name of
Servlet and the Servlet mapping to a URL. This will allow us to access our
Servlet using a URL like /TestServlet.
To run a Servlet
Now we need to start Tomcat server. Type the following command
at DOS prompt and hit Enter to start the Tomcat server:
Now open your browser and point to this address:
http://localhost:8080/star/Testservlet. You should a get response like following
image in your browser window:
We began with the introduction to Java Servlets and learned that
Serlvets are simple Java classes that implement javax.servlet.Servlet
interface. In practice, we will most probably do that by extending
javax.servlet.GenericServlet or javax.servlet.http.HttpServlet classes.
By above discussions we get know that servlet is efficient and extensible as it
store once .
in generic servlet service method cant be implemented..
this is wrong ..
Show All Comments