Wednesday, July 8, 2009

servlet-interface

In javax.servlet.Servlet Interface contains the following methods:

public void init (Servletconfig config) throws ServletException.

  • This method called by servlet container to indicate to a servlet that the servlet is being placed into the service.
  • The init () method called by servlet container exactly once after instantiating the servlet.
  • We can override init () method if we have initialization code like getting database connection.
  • The servlet container can not place the servlet into service of the init () methods.
In the following situation the web container destroys that servlet object.
If it throws a servlet exception
If it doesn't return with in a time period defined by the web server.

public void service (servlet Request req, Servlet Response res) throws ServletException, IOException :
  • Called by servlet container to allow to respond to the client request.
  • It should be called after the servlet’s init()method completion

public void destroy() :
  • The servlet container call the destroy () method before removing a servlet instance from service.
  • This normally happens when the servlet container is shut down of the servlet container needs some free memory.
  • This method is only called once all the thread with in the servlet service () method have exited.
  • We can keep cleanup code inside destroy () method.

public servlet config getservletconfig() :
It returns servlet config object which contain initialization and start of information of the servlet object.

public string getServletInfo() :
It returns a string containing information about the servlet such as author version and copy right.


Sample servlet program by implement Servlet interface :


import javax.servlet.*;
import java.io.*;
class BasicServlet implements Servlet
{
public void init ()(Servlet config conf) throws ServletException
{
System.out.println (“input method”);
}

public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException
{
System.out.println (“service method”);
}

public void destroy ()
{
System.out.println (“destroy method”);
}

public ServletConfig getServletConfig ()
{
return null;
}

public String getServletInfo ()
{
return null;
}

public String getServletInfo ()
{
return null;
}
}

0 comments:

Post a Comment