Servlet Interface  
public interface Servlet  
 

Every servlet must implement the Servlet interface. It declares the methods that govern the life cycle of the servlet as well as methods to access initialization parameters and information about the servlet

Example: A Simple Servlet

This example creates a simple servlet that returns the current date and time. It overrides the service() method to write the string representation of a Date object back to the client machine. The output from the servlet is set to be interpreted as HTML code.

 
            import javax.servlet.*; 
     import java.io.*; 
     import java.util.*; 
     public class SimpleServlet extends GenericServlet { 
       public void service(ServletRequest request, ServletResponse response) 
               throws ServletException, IOException { 
         response.setContentType("text/html"); 
         PrintWriter pw = response.getWriter(); 
         Date d = new Date(); 
         pw.println("<B>The date and time are " + d.toString()); 
         pw.close(); 
       } 
     } 
destroy()  
public void destroy() Method
 

destroy() unloads the servlet from the server's memory and releases any resources associated with the servlet.

getServletConfig()  
public ServletConfig getServletConfig() Method
 

getServletConfig() returns the ServletConfig object associated with the servlet. A ServletConfig object contains parameters that are used to initialize the servlet.

getServletInfo()  
public String getServletInfo() Method
 

getServletInfo() returns a String containing useful information about the servlet. By default, this method returns an empty String. It can be overridden to provide more useful information.

init()  
public void init(ServletConfig config) throws ServletException Method
 

init() is called when the servlet is loaded into the address space of the server. The ServletConfig object is used to provide the servlet with initialization parameters.

service()  
public abstract void service(ServletRequest request, ServletResponse response) throws ServletException, IOException Method
 

service() is called to respond to a request from a client machine. The code representing what the servlet is supposed to do is placed in this method.