Tuesday 28 August 2018

Servlet Interface

Servlet Interface,Servlet lifecycle,void destroy(),void init(ServletConfig config),void init(),void service(ServletRequest req, ServletResponse res),void service(),ServletConfig getServletConfig(),java.lang.String getServletInfo(), example of servlet interface,

Servlet interface provides common behaviour to all the servlets.
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.

Servlet Interface Method
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.
      
    1. void destroy(): This method is called by Servlet container at the end       of servlet life cycle. Unlike service() method that gets called multiple times  during life cycle, this method is called only once by Servlet container during  the complete life cycle. Once destroy() method is called the servlet container does not call the service() method for that servlet.
    2. void init(ServletConfig config): When Servlet container starts up (that happens when the web server starts up) it loads all the servlets and instantiates them. After this init() method gets called for each instantiated servlet, this method initializes the servlet.

      3. void service(ServletRequest req, ServletResponse res): This is the only method that is called multiple times during servlet life cycle. This methods serves the client request, it is called every time the server receives a request.

     4. ServletConfig getServletConfig(): Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.

    5. java.lang.String getServletInfo(): Returns information about the servlet, such as author, version, and copyright.


Servlet Example by implementing Servlet interface

Let's see the simple example of servlet by implementing the servlet interface.
File : Dashzin.java
       
        import java.io.*;  
        import javax.servlet.*;  
  
        public class Dashzin implements Servlet{  
        ServletConfig config=null;  
  
       public void init(ServletConfig config){  
       this.config=config;  
       System.out.println("servlet is initialized");  
       }  
  
       public void service(ServletRequest req,ServletResponse res)  
       throws IOException,ServletException{  
  
      res.setContentType("text/html");  
  
      PrintWriter out=res.getWriter();  
       out.print("<html><body>");  
        out.print("<b>hello simple servlet using servlet interface</b>");  
       out.print("</body></html>");  
  
        }  
        public void destroy(){
        System.out.println("servlet is destroyed");
        }  
        public ServletConfig getServletConfig(){
       return config;
       }  
        public String getServletInfo(){
        return "Welcome to Dashzin Servlet Blog";
       }  
       }  










DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment