Thursday 29 March 2012

Servlet Chaining


 Servlet Chaining

This topic has been included in J2EE(Java to Enterprise Edition i.e Advance Java) Here as discussed earlier, output of one servlet act as a input to another servlet and at last request is sent to the browser and final output is achieved. Servlet allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet.the process continues as follows-



·        Two Ways to Chain Servlets

In Servlets/JSPs, there are two ways to achieve servlet chaining using


RequestDispatcher:

RequestDispatcher is used to dispatch the request URL to execute another servlet in servlet chaining. Process goes on this way that first servlet directly dispatch the request to execute another servlet by dispatching URL. There are two methods:
request.include()  and request.forward()
The above methods are used to dispatch the request for another servlet execution. In this process, client or browser not involved in request dispatching..Here, all servers are executing in same server and client is not participating in dispatching thus  servlet chaining is fast. In this process we cannot communicate with multiple servers. We can use either of the method.



RequestDispatcher rd=new RequestDispatcher();
rd.forward(/”index.jsp”);
or
RequestDispatcher rd=new RequestDispatcher();
rd.include(/”servletTwo.jsp”);


Diamgramatic-explanation:

Here, as shown request of servlet 1 is forward to Servlet 2 and further to Servlet 3. This way the process continues.
Advantage:
I.. Same url connection is used for communication between servlets.
ii. All servlets are executing in the same server.
iii. This servlet chaining process is faster than rendRedirect.
iv. Client is not involved in this process.

Limitation:
In this process we cannot send the request to other servlet that is executed in the other server i.e  limitation we cannot  communicate with multiple servers.



Include



Here, we are using include method i.e Servlet 2 is included in Servlet 1 and Servlet 3 is included in Servlet 2 and chaining goes in this manner.The output will remain same either method can be used



·        Is there any difference

output of one servlet act as a input to another servlet and at last request is sent to the browser and final output is achieved. This is called servlet chaining.


output of one JSp page act as a input to another JSP page and at last request is sent to the browser and final output is achieved.Is this also Servlet chaining.???
Yes, this is also called servlet chaining because jsp page gets converted into Servlet at last by servlet engine that’s the reason.For more clearance watch out the below demos…



























1 comment:

  1. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.
    Java Courses in chennai

    ReplyDelete