Servlet Interface
Interface that defines Servlet is called Servlet Interface and the implementing class for Servlet much override all existing method of a Servlet Interface.
For creating Servlet it needs to implement Servlet Interface.
It is an interface to define a Servlet, the implementation class of this Servlet should override all methods of Servlet interface. There are 3 methods or we can say life cycle which are done to initialize Servlet for requesting services, kill servlet and 2 non-life cycle methods.
Servlet Interface Methods
Methods | Description |
public void init(ServletConfig config) | initializes the Servlet. It is the life cycle method of Servlet and invoked by the web container only once. |
public void service(ServletRequest request,ServletResponse response) | Generate response for in servlet request and web containers starts each request.is invoked at each request by the web container. |
public void destroy() | is invoked only once and indicates that Servlet is being destroyed. |
public ServletConfig getServletConfig() | returns the object of ServletConfig. |
public String getServletInfo() | returns information about Servlet such as writer, copyright, version etc. |
Example of servlet by implementing Servlet interface
Syntax public class myServlet implements server { .... } public void destroy() { ..... } public void init(ServletConfig se) { ..... } public ServletConfig getServletConfig() { ..... } public String getServiceInfo() { .... } public void service(ServletRequest req, ServletResponse resp)throws IOException, ServletException { .... }