Home Servlets Methods of Servlets
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Methods of Servlets

Advertisement
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.

Methods of Servlets

     

A Generic servlet contains the following five methods:

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. 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 18, 2008

Ask Questions?    Discuss: Methods of Servlets   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
vahid
April 5, 2011
init method

when init() method will invoked? after invoking service() methode or befor invoking service() method?
Viny Sharma
June 18, 2011
queries related to advance java

hi.. it was my first day of servlets my teacher made two basic programs of servlets but in second he didn't use init() method to initialize the servlet and it still worked so how will the servlet get started ? thanks
sai
May 30, 2013
Is It possible To write like This

public void init(ServletRequest req, ServletResponse res) throws ServletException,IOException
Antu
February 14, 2012
java sevlets

how to make the factorial system through servlet in java and how to avoid to "NUMBER FORMAT EXCEPTION". please solve it through the servlets .
manish chakravarti
February 16, 2012
javax.servlet

The above five is not a generic servlet it is a servlet interface .. The generic servlet contain 11 methods or generic servlet implement three interface.
Amit
March 1, 2012
SErvlet

is it possible to think? class servletExample { void serVlet(){ void doGet(HttpServletRrequest request,HttpServletRespose response)throws IOException,ServletException{ ...} } }
mohan
April 26, 2012
java

what is getparamethod ,why we use this method
kumar
May 17, 2012
About RoseIndia

please remove the cartoon yourself add..it is irritating..it is disturbing when browse your site
sasasa
May 30, 2012
java

its good
saket
June 8, 2012
two overloaded forms of servlets service methods

what are the two overloaded forms of servlets service methods and what are their significance?
ashish
July 10, 2012
good

gooood sir
jvdprasad
July 31, 2012
enterprise programing

i am not understading this subject
DMCA.com