Image Source  Google Images Happy Christmas


                        Hi All my blog viewers Happy Christmas. To my post concentrating on inter servlet communication.Before going to to how it works inter servlet communication. Some of the important points you need to know.

1.       Servlet Context
2.      Request Dispatcher Object.

1.       Servlet Context: -  Servlet Engine create ServletContext object as soon as the web application is deployed. This object is only one per web application. All Servlets share this object. ServletContext object is not directly available for servlets. To get its reference we have to call the following method.  

                        ServletContext sc = getServletContext();

Even though the above method is called on the servlet instance, internally it is called on the ServletConfig object. I.e., getServletContext method belongs to ServletConfig object.

ServletContext object can be treated as shared informatory repository for application level data. One servlet can store data into this object and another servlet of the same application can access  that data. A Servlet uses this object for the following purposes.

1.       To Communicate with the web container
2.      To talk to other servlets
3.      To Share data (in the form of attributes) with other servlets
4.      To get application level initialization parameters known as context parameters.

Whenever a servlet wants to know the web container details in which it is deployed, it can call getServletInfo method on the “sc”(ServletContext object. To write any information into container log files we call log method on “sc”. One servlet can store a data item into the ServletContext object by calling setAttribute method. Another servlet can get it by calling getAttribute method on the “sc” object.Whenever a servlet wants to communicate with the other servlet, call getRequestDispatcher method on the “sc” object.It produces RequestDispatcher object. Using RequestDispatcher object one servlet can communicate with other servlets of the web application. Initialization parameters are individual for each servlet. They are supplied from web.xml. Aservlet retrieves them by ccalling getInitParameter() on the ServletConfig object. When we want to supply common init parameters for all the servlets we use context parameters.

<context-param>
            <param-name>any name</param-name>
            <param-value>anyvalue</param-value>
</context-param>

RequestDispatcher object:

 RequestDispatcher object is used in the servlets to implement request dispatching. A Servlet recives the client request, does the processing partially and hands over the request processing duty to another servlet. This mechanism is known as request dispatching. Inter servlet communication is implemented using RequestDispacher object. In a servlet we can get RequestDispatcher object reference in two ways.

1.       RequestDispatcher rd = context.getRequestDispatcher(String absolutepath);
2.      RequestDispatcher rd=request.getRequestDispatcher(String relativepath);

If we are using the ServletContext object to get the RequestDispatcher, we have to give absolute URL of the target resource. If we are using HttpServletRequest to get the RequestDispatcher object, we give only relative url of the target resource.Request dispatching can be implemented in two ways.

1.       Forward mechanism
2.      Include mechanism

You can see these functionality and all the following video in better way.


See my Next post how to handle Servlet Exception…..

Post a Comment

 
Top