What is meant b Servlet and it's Methods?
>> Wednesday, June 1, 2011
Servlet:It is an object ,which can be used to generate dynamic response from server.
Methods In Servlet:
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
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
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
5.)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.
Methods In Servlet:
1.)public void init(ServletConfig config) throws Servlet Exception
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
2.)public void service(ServletRequest request,ServletResponse response) throws IO Exception,servlet Exception
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.
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.
3.)public String getServletInfo();
Returns - the ServletConfig object
4.)public ServletConfig getServletConfig();
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
5.)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.
0 comments:
Post a Comment