Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com

Web Service

Servlet Basics

  • The basic structure of servlets

  • A simple servlet that generates plain text

  • A servlet that generates HTML

  • Servlets and packages

  • Some utilities that help build HTML

  • The servlet life cycle

  • Servlet debugging strategies

What is a servlet

  • Servlet is a web component made  in java language.It is a java class  which runs on the java web server.
  • It uses mostly Http protocol to run the program.It process the client request and then give the response to the client.

The basic structure of servlet

Structure of the  servlet application

                  | html,jsp,servlet,image file
         At top of the servlet-app there are java,html and jsp files.It contains WEB-INF directory.This directory contains a classes  directory and a web.xml file.

web.xml is the deployment descriptor file.
It has the description of all the  servlets used in the given web application

Server for servlet
Servlet application runs on the web server as well as on most application server.
  • Main  web servers availbale in the market 
    are tomcat, jetty and  java web server.
  • Tomcat is most used web server.
  • It is the produact of apache company.
  • It contains a servlet engine which contains the web container.
  • Web container executes the servlet
  • Application server for servlets execution are 
    Jboss, Weblogic, Glassfish, Websphere,Oracle application server.
Servlet Api and Types

  • Servlet is the java file used for java web programming.
  • Sun has developed separate api for the  web development in
    the form of servlet and jsp.
  • In the servlet api Servlet is an interface.
  •  It is present in the package javax.servlet.

servlet api is present in the  two packages
these are
  1. javax.servlet
  2. javax.servlet.http

Heirarchy of the servlet is


  • Servlet is an interface having basic methods for servlet.
  • This servlet interface is implemented  by the generic servlet claass.
  • The methods of the servlet interface is overriden by the Generic servlet.
  • Generic servlet is again extended by the HttpServlet.

Servlet interface has  main methods  for initialization, service and destroy

  • void init(ServletConfig)
  • void service()
  • void destroy()
  • getServletInfo()
  • getservletConfig(ServletRequest,ServletResponse)

These methods makes the life cycle of the servlet.


It implements the Servlet Interface.This class is responsible for making the servlet program. It runs the servlet on all major protocols used such as ftp, smtp, http.
It has two constructor of the service method.It is present in the javax.servlet package.


This class extends the GenericServlet.It is specific to the http protocol.
It is the most used class in the servlet.It is present in the javax.servlet.http package.
It contains the methods such as  doGet(),doPost(),doDelete(),doHeader() etc.

 Methods  are :-
  • doGet()
  • doPost()
  • doDelete()
  • doHeader()
  • doPut()
  • service()
It has two overridden version of service() method
  • protected  void service(HttpServletRequest req, HttpServletResponse resp)
  • public  void  service(ServletRequest req, ServletResponse res)
  • First client request comes to the public void service() method
  • It then  dispatches client requests to the protected service method.

  • protected service() method receives HTTP requests from the public service method and dispatches them to the do...() methods defined in this class.

So the  calling functions flow is like this
client -->public service()-->protected service-->doXXX()

  • This method is used for handling the get reques.it is called by the service methods.
  • This suports the HTTP HEAD request.
  • HEAD request is a GET request.
  • It returns no body.
  • It returns the request header fields.
  • In this methods values passing from form to the server gets added to the url.
  • It allows limited data to be passesd

  • This method is used for handling the post request.
  • It is called by the service methods.
  • In this methods values passing from form to the server gets added to the body instead of the url.
  • Using the  HTTP post  method the user can send data of unlimited length to the Web server.
  • It is used mostly as the information sending is more secure sucha as credit cared, debit card information.

Servlet Life Cycle

It consists of three phases
  1. Loading, Instantiation and Initialization
  2. Operational
  3. Finalization
Loading, Instantiation and Initialization
  • The Servlet is loaded into the Servlet Container/Web Server.
  • An instance of the Servlet class is created.
  • The container calls the Servlet’s init method. The init method is usually overridden to acquire resources.    
  • The container invokes the Servlet’s service method, passing request and response objects.
  • The javax.Servlet.http.HttpServlet class contains more specialized service methods [such as doGet and doPost] for handling different types of HTTP requests.

  • The container calls the Servlet’s destroy method. Overriding this method releases the resources acquired by the Servlet.

Initializing a Servlet

  • The container calls the init method of the Servlet once to initialize it. A ServletException is thrown if initialization fails.
  • Fortunately, we no longer have to implement this method since the GenericServlet class already has one. In cases where customization is absolutely necessary, GenericServlet also has an overloaded init method to further simplify the process – no need to call super.init(ServletConfig config).

The following example overrides the overloaded init method:

    public class Hello extends HttpServlet {
        public void init() {   
            System.out.println(“Initializing Servlet…"); 

Writing Service Methods

  • The service provided by a Servlet is implemented in the service method of a GenericServlet, in the doMethod methods (where Method can take the value Get, Delete, Options, Post, Put, or Trace) of an HttpServlet object or in any other protocol-specific methods defined by a class that implements the Servlet interface.
  • The doGet, doDelete, doOptions, doPost, doPut, and doTrace methods are called by the server (via the service method) to allow a Servlet to handle GET, DELETE, OPTIONS, POST, PUT, and TRACE requests respectively.

Maintaning Client State

  • Web-based applications are responsible for maintaining state, called a session, in order to associate a series of HTTP requests from a client.
  • Java Servlet technology provides an API for managing sessions and allows several mechanisms for implementing sessions.
  • Sessions are represented by an javax.Servlet.HttpSession object.
  • The getSession method of request objects is used to retrieve the current session associated with this request, or, if the request does not have a session, it creates one. 
  • Because there is no way for an HTTP client to signal it no longer needs a session, each session has an associated timeout so that its resources can be reclaimed. The timeout period can be accessed by using a session's [get|set]MaxInactiveInterval method. You can also set the timeout period using the deploy tool.
  • When a particular client interaction is finished, you use the session's invalidate method to invalidate a session on the server side and remove any session data.