Servlet Interface


Prev Tutorial Next Tutorial

Servlet Interface

It is an interface to define a Servlet, the implementation class of this Servlet should override all methods of Servlet interface.
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.

Methods of Servlet interface

MethodDescription
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) provides response for the incoming request. It 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
{
....
}

Example




Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements