It2EDU

Saturday, October 29, 2016





Servlet is a java class implements server functionality. Java Servlet technology lets you define HTTP-specific
servlet classes. A servlet class extends the capabilities of servers that host
applications accessed by way of a request-response programming model. Although
servlets can respond to any type of request, they are commonly used to extend
the applications hosted by web servers. Java servlets are run inside the web
server. Servlets are used to build web applications.  
Actually Servlets are replacement of
CGI (Common gate way interfaces). In CGI every request creates an instance and
process the request.  So the server takes a lot burden and it is affected
on server efficiency and performance. 











Servlet
features and advantages:







  • Servlets are small platform independent java programs.

  • Used for developing web applications.

  • Robust, portable and secure.

  • Servlet are faster than others like CGI.

  • Servlets uses standard API that supported by many web servers.

  • Servlets executes within the address space of a web server.

  • Servlets does not create instances for each request.
















Discussion
about the servlet life cycle and methods:


The lifecycle of a servlet is controlled by the container in which the
servlet has been deployed. When a request is mapped to a servlet, the container
performs the following steps.




     
If an instance of the servlet does not exist, the web container Loads the
servlet class.


b    Creates an
instance of the servlet class.


c   
Initializes the servlet instance by calling the init method. Initialization is
covered in Creating and initializing a servlet.



2    Invokes the
service method, passing request and response objects. Service methods are
discussed in Writing Service methods.


If it needs to remove the servlet, the container finalizes the servlet by
calling the servlet’s destroy method.










The init() method:










The init() method is called during initialization phase of the servlet life
cycle. The Web container first maps the requested URL to the corresponding
servlet available in the Web container and then instantiates the servlet. The
Web container then creates an object of the ServletConfig interface, which
contains the startup configuration information, such as initialization
parameters of the servlet. The Web container then calls the init() method of
the servlet and passes the ServletConfig object to it. ServletException is thrown by init() method in the
context of web container doesn’t initialize the resources of servlet.







The service() method:








 The service() method is the
important method, it contains the operation code to process the request.

The servlet container calls the service() method to process the request and
generate response to the client. Each time the servlet receives the request and
process it and generates the response. This process is going on until it call
the destroy() method.

The service() method is breaks into doGet(),
doPost(),doDelete(),doOption(),doPut() and doTrace(). Most commonly used
methods are doGet() and doPost().

  








The destroy() method:





The destroy() method is called when we want to shut down the application.
When the destroy() method is called the servlet resources are removed from the
web container.

Servlet init() and destroy() methods called once during execution of a
servlet.









Below picture shows the servlet lifecycle:























See the below example for better understanding about servlet lifecycle
methods:



import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import
javax.servlet.http.HttpServletRequest;


import
javax.servlet.http.HttpServletResponse;


import java.io.PrintWriter;



/**

 * Servlet
implementation class ServletMethodsDemo


 */

@WebServlet("/ServletMethodsDemo")

public class ServletMethodsDemo extends HttpServlet {

      
private static final long serialVersionUID = 1L;

      


    private String name="Santosh kumar G";

   

    public void init()throws ServletException{

      
System.
out.println("init() method is executed");

    }

      


    public void destroy(){

      
System.
out.println("Destroy method executed");

    }



       /**

      
 *
@see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)


      
 */


      
protected void
doGet(HttpServletRequest
request, HttpServletResponse response) throws
ServletException, IOException {


             
//
TODO
Auto-generated method stub


             


             
//response.getWriter().append("Served
at: ").append(request.getContextPath());


             
response.setContentType("text/html");

             
PrintWriter
out =response.getWriter();

             
//to
print the content


             
out.println("<html><head>");

             
out.println("<title>Demo
Servlet(by"
);

             
out.println(name);

             
out.println(")</title>");

             
out.println("</head>");

             
out.println("<body><p>Demo
Servlet (by"
);

             
out.println(name);

             
out.println(")</p></body></html>");

             
out.close();

                          


      
}




      
/**

      
 *
@see
HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)


      
 */


      
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

             
// TODO Auto-generated method stub

             
doGet(
request, response);

      
}




}



We can override init(),destroy(),doGet() and doPost() methods in this
example.

When we start the servlet, it first calls the init method and then go to the
doGet() method and process the requests. Once the servlet instance removing
from the container destroy() method executes.