5 method of Servlet

init()
-----------------------
public void init(ServletConfig config) throws ServletException
The init() method is called only once by the servlet container throughout the life of a servlet. By this init() method the servlet get to know that it has been placed into service.
The servlet cannot be put into the service if
The init() method does not return within a fix time set by the web server.
It throws a ServletException
Parameters - The init() method takes a ServletConfig object that contains the initialization parameters and servlet's configuration and throws a ServletException if an exception has occurred.

service()
----------------------
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException

Once the servlet starts getting the requests, the service() method is called by the servlet container to respond. The servlet services the client's request with the help of two objects. These two objects javax.servlet.ServletRequest and javax.servlet.ServletResponse are passed by the servlet container.

The status code of the response always should be set for a servlet that throws or sends an error.

Parameters - The service() method takes the ServletRequest object that contains the client's request and the object ServletResponse contains the servlet's response. The service() method throws ServletException and IOExceptions exception.



getServletConfig()
---------------------------
public ServletConfig getServletConfig()
This method contains parameters for initialization and startup of the servlet and returns a ServletConfig object. This object is then passed to the init method. When this interface is implemented then it stores the ServletConfig object in order to return it. It is done by the generic class which implements this inetrface.
Returns - the ServletConfig object

getServletInfo()
-------------------------
public String getServletInfo()
The information about the servlet is returned by this method like version, author etc. This method returns a string which should be in the form of plain text and not any kind of markup.
Returns - a string that contains the information about the servlet

destroy()
----------------------
public void destroy()
This method is called when we need to close the servlet. That is before removing a servlet instance from service, the servlet container calls the destroy() method. Once the servlet container calls the destroy() method, no service methods will be then called . That is after the exit of all the threads running in the servlet, the destroy() method is called. Hence, the servlet gets a chance to clean up all the resources like memory, threads etc which are being held.

Example :
------------
1. SetterServlet.java
------------------------------
import java.io.*;
import javax.servlet.*;

public class SetterServlet extends GenericServlet
{
ServletConfig config;
String driver;
String dsn;
public void init(ServletConfig config) throws ServletException
{
this.config=config;
driver=config.getInitParameter("DRIVER");
dsn=config.getInitParameter("DSN");


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

PrintWriter out=res.getWriter();
System.out.println("service method run properly");
res.setContentType("text/html");

ServletContext context=config.getServletContext();

context.setAttribute("DRIVER",driver);
context.setAttribute("DSN",dsn);

String dr,ds;
dr = (String)context.getAttribute("DRIVER");
ds = (String)context.getAttribute("DSN");

out.println("driver is:-"+dr);
out.println("
dsn is:-"+ds);
}
}

2.GetterServlet.java
-----------------------------
import javax.servlet.*;
import java.io.*;

public class GetterServlet extends GenericServlet
{
String dr,ds;
public void init(ServletConfig config) throws ServletException
{
ServletContext context = config.getServletContext();
dr = (String) context.getAttribute("DRIVER");
ds = (String) context.getAttribute("DSN");
}
public void service(ServletRequest req,ServletResponse res) throws IOException,ServletException
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");

out.println("DRIVER : "+dr);
out.println("
DSN : "+ds);
}
}

user init-param,param-name and param-value in XML for servlet

1 comment:

  1. how to insert or call a method in netbeans j2me?? we are developing a mobile application text to speech..pls,,reply asap

    ReplyDelete