UnderStanding the Servlet's methods.......

Servlet.

A servlet is sun specification api to develope web application.It has been implemented based on opps and multithreading concept of java.
It's come under JavaEE specification and the vendor are responsible to implemen it that specification

The root interface of servlet  from where servlet start  is "Servlet"
It contains method

public abstract void init(ServletConfig config) throws ServletException

 
public  abstract void service(ServletRequest req,ServletResponse res)
                                              throws ServletException,IOException 
 
public abstract  void destroy() 
 
public abstract  String getServletInfo()
 
public abstract ServletConfig getServletConfig()
 
The  above method are the core method of servlet specification.
The vendor or user who want to develop web application  he should have to implement
those method in their class to get sevlet  behavior.


Life cycle Of Servlet

  • Instantiation

  • Initialization

  • Service

  • Destroy

    Instantiation

    It is first phase of servlet in which servlet object will be created  by the  container and necessary memory area will be reserved for the entire request processing.

    Initialization

      It is the next step of servlet in which servlet object will be populated by servlet config object.In otherwords all the servlet initialization parameter which will be declared under param tag inside the web.xml file be available to the servlet object in the form of ServletConfig object.
    ServletConfig object is the right hand of servlet.

    Service

    It is the most important method of servlet specification throgh which entire request processing has to
    carried out.It is called whenever a particular request will be made by the user.For every request servlet will create a Thread to serve the request and will produce response . 

  ServletConfig

ServletConfig object is the right hand of Servlet object and it is created at the time of servlet object  creation .
The hierarchy is ...

 

Whenever servlet  object will be created automatically ServletConfig object will  be created by container.
It is helper object which help the  servlet to keep the internalization parameter which  will be provided through web.xml or though annotation .
For every Servlet class one ServletConfig object will be created independently and will remain till destroy method will be called.I will be explaining more.......in coming post..

Following are the method which  are called life cycle method of Servlet which resides under Servlet Interface.

init(ServletConfig config)

This method is the container call back method and will be called after servlet object instantiation.It carries all the initialization parameter which will configured in deployment descriptor or in annotation  as a meta data information.
When this method will be called all the initialization parameter will be available to servlet object so that it can get all the parameter name and value  to perform some operation.For each servlet one ServletConfig object.
This method will be called once and is responsible to perform initialization only.

Service(ServletRequest req,ServletResponse res)

 This is the life cycle method of servlet and will be called for every request made by user.It is the container call back method and is automatcally called by container in during servlet life cycle.
For every request to service method one thread will be created by container for request processing and each thread will be processing their request independently simultaneously.


Request processing in Servlet is based on multithreading in will all request will served at same time to increase the performance.

Destroy

This the life cycle method of servlet in which servlet object wiill be destroyed by the garbage collector to destroy the servlet object which has been created during instantiation process to delete the memory space.

getServletConfig()

This is the life cycle method of servlet in which we will be getting the Servlet Config object which will be created during the instantiation.

getServletInfo

This method will be called by conatiner automatically as it is the  cotainer call back method and will be giving much information about Servlet object..

Adopter Design Pattern.

It is the design pattern which is used to provide abstraction and is called Helper design pattern.
If a interface contains multiple method  and user wants only few method  then we will use this design pattern.
In otherwords instead of implemementing all the interface method in single class,It is better to provide one helper class who will implement that interface by providing body to all method and we will extend that the implemetation class as per our method requirement.It increases the flexibility.

Servlet specification has given once class called GenericServlet who has implemented all the Servlet interface method and does not contain any data inside the implemented method for user perspective point of view.
The user who want only service method ,he has to to override the service method and can make a request upon servlet.

GenericSevlet

It is the protocol independent Servlet class which provides implementation on Servlet interface and tell to user to override the method as per the requirement.We can get the servlet behavior in our class by extending GenericServlet class. 

HttpSevlet

it is the most important class of Servlet specification which is a protocol dependent servlet class.This class is used for performing HTTP operation.
This class is specially user for following operation.

  • UrlRewriting
  • Cookies
  • Securities. 
  • Session Management
Basically HttpServlet is famous for Session Tracking.
This class provides many method to provide different type of operation with server.
HttpServlet Following most commonly used methods
doGet()
doPost()
doPut()
doOption()
doDelete()
doTrace()
doHead()
init()
 The most commonly used method are doGet(),doPost() and doHead()
doGet() is used for requesting a particuler resouce from server and send minimum amount of data to the server.
doPost() is used for   getting resource from server and also used for send maximum data to sever This method  can change the content of server.

doHead() is used to check the  header content of a particuler resouces which exist in server side.In otherwords this method is used to check the resource content for proper request response communication.

init() is the user defined method available in HttpServlet specially to be called by User defined servlet class.
HttpServlet contain two service method.
  • public void service(ServletRequest request,ServletResponse response)
  • protected void service(HttpServletRequest request,HttpServletResponse response)

public service(ServletRequest request,ServletResponse response)

It is the method of Servlet interface which has been implemented by   GenericServlet class  directly.
It is overridden by HttpServlet class  and so on...

 protected void service(HttpServletRequest request,HttpServletResponse response)
This method is available only in HttpServlet class and is responsible to process all Http request response operation.
This method is calling public service method of Servlet interface  which has been implemented  in GenericServlet class.

 Service method

Code inside HttpServlet service method..
public abstract  class GenericServlet implements Servlet
{
public void init(ServletConfig config)
{
}
 public  abstract void service(ServletRequest request,ServletResponse response);
}
public void destroy()
{
}
public ServletConfig getServletConfig()
{
}
public String getServletInfo()
{
}
...
public abstract  class HttpServlet extends GenericServlet
{
public void service(ServletRequest request,ServletResponse response)
{
}
}
protected void service(HttpServletRequest request,HttpServletResponse response)
{
 service( request,response);
}


init() method

Whenever user will make a request to Servlet for first time,Container will instantiate the Servlet object by executing  the zero argument constructor.
After servlet object creation ServletConfig object will be created by container and it will read  all the intialization parameter from deployment descriptor and will keep in the ServletConfig object.

Container calls the init method of Servlet .It check whether user defined servlet class is extending HttpServlet class or not.If no then it call init method with ServletConfg as argument method of Servlet interface and completes the intialization process.

If used then it will call the HttpServlet class init method with ServletConfig as argument(init(ServletConfig config))and process..

If user defined servlet class has init method with zero argument then the HttpServlet init() method will be called which does not contain any argument  and is calling init(ServletConfig config) of Servlet interface. 

there are thre way we can declare we can use init method

init(ServletConfig config)
{
}
merit: we can directly get the ServletConfig object in our class
demerit: we can not store ServletConfig object for future use.i mean if we call getServletConfig() in our class we will get null.

ServletConfig config;
init(ServletConfig config)
{
 super.init(config);
}
To get the ServletConfig object outside the init method we can call super (HttpServlet)call init method which indirectly call init(ServletConfig config) of GenericServlet and initializes the ServletConfig object.

init()

This the most convenience method available in HttpServlet .If user uses that method then container check whether any init method  is present in User defined servlet class or not.if not then it will call super class init method. the super class init() method(zero argument ) call s init(ServletConfig config ) ,initializes the ServletConfig object and then again  calls the init()(zero argument) method of HttpServlet class.

The init() methid(zero argument) of HttpServlet is empty body and  if user has overriden init method then the overridden method will be called and ServletConfig object will be available all method of the user defined servlet class.
 code are like
  
public abstract class  GenericServlet implements Servlet
{
public void init(ServletConfig config)
{
//initialize the ServletConfig object//
}
 public  abstract void service(ServletRequest request,ServletResponse response);
}
public void destroy()
{
}
public ServletConfig getServletConfig()
{
}
public String getServletInfo()
{
}public abstract class HttpServlet extends GenericServlet
{
init(ServletConfig config)
{
super.init(config);
init() ;

init()
{
//empty body
}
}

public class MyServlet extends HttpServlet
{
init() //overridden method..
{
///your code........///

.....


ServletConfig


Is the servlet specification interface which has been provided for storing and retriving initialization information of a particular servlet or jsp.
This is the right hand object of servlet and is created during the servlet object instantiantion.

For each servlet a unique ServletConfig object will be created and will remain till the servlet object gets destroyed.
we can intialize the ServletConfig object 's parameter through web.xml . 
in web.xml....we can configure like......
<servlet>
<servlet-name>..</servlet-name>
<servlet-class>...</servlet-class>
<init-param>
<param-name>helllo</param-name>
<param-value>welcome to india</param-value>
</init-param> 
</servlet>
and in java class we can access like.

Servlet Config config=getServletConfig().getInitParameter("hello");

and the output will be welcome to india.
In otherwords we will be giving the param name and value in the form of key value pair of Hashmap object.

methods available in ServletConfig interface.


 java.lang.String getInitParameter(java.lang.String name)
          Gets the value of the initialization parameter with the given name.
 java.util.Enumeration<java.lang.String> getInitParameterNames()
          Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
 ServletContext getServletContext()
          Returns a reference to the ServletContext in which the caller is executing.
 java.lang.String getServletName()
          Returns the name of this servlet instance.

  
If we are overriding the init()(no argument) method in our servlet class then we can access ServletConfig object in any method of our class.
If we are overriding init (ServletConfig config) method in our class then we have to call super class method to
make ServletConfig object  available in other method  like service method else we will get null .....

public void init(ServletConfig config)
{
super.init();
}

3 comments: