Thursday, 29 March 2012

Inter Servlet Communication



 Inter Servlet Communication


As the name comes in mind i.e Inter Servlet Communication , one can understand it as it is communication between two or more servlets. Servlets talk to each other. There are many ways by which Servlets can communicate, some of them are mentioned as follows:
§  Request Dispatching
§  HTTP Redirect
§  Servlet Chaining
§  HTTP request (using sockets or the URLConnection class)
§  Shared session, request, or application objects (beans)
§  Direct method invocation (deprecated)
§  Shared static or instance variables (deprecated)
Basically interServlet communication can be acheived through servlet channing..


As shown above, we are given 3 servlets , output of Servlet 1 act as input to servet 2 similarly , output of Servlet 2 act as input to Servlet 3 .This process is called Servlet Chaining.Many servets can communicate to each other using same URL. Servlet can use HTTP request rather than direct calls.One Servlet can call other servlet by sending an Http request by opening same url connection. The Output from the last Servlet is sent back to the browser. To understand in more better form below diagram is given. 

·         How do JSP page communicate with Server
Step1:- Design JSP page



Step2:-Select the items



Step3:-Click on Add To Cart and the values will get on servlet as shown



·        Camparison of JSP and Servlet on basis of Performance




JSP PAGE
SERVLET


Here, we can experience a delay
When JSP page is accessed first time.
Here, no delay is experienced


It undergoes a translation phase where
Jsp page is converted into servlet by
JSP engine.
Once, servlet is loaded in the memory
It follows the life cycle for request processing. 










                                   Jsp Page




Servlet



Servlet Mapping

This mapping is specified with the help of servlet name in web.xml file, we can map servlet class with url pattern. In web.xml we have two child tags <servlet-name> and <url-pattern>.<servlet-name> specifies the name of the servlet, which is called for incoming URL pattern matching by giving values in<url-pattern> element Every<servlet> element  have two child elements - <servlet-name> and<servlet-class>. Servlet name and url pattern should be same as shown.







Definining Servlet Mapping



Location where web.xml is stored

Web xml is made in webapps àWEB-INF à web.xml(save here).
As per the folders shown above we can save our files accordingly like html file in META-INF and similarly jsp page as well as css files, images in images folder similarly scripts.Any program we want to make can be stored in classes the compiled from there.





Implicit  Mapping


Servet can contain JSP(.jsp) extensions we map jsp page to servlet with the help of servlet engine and jsp page is converted into servlet this is called Implicit Mapping. Servlet Mapping and Implicit Mapping are different as in servlet mapping we use url pattern for mapping of servlet whereas here in implicit mapping its automatic i.e jsp pg is converted into servlet with the help of servlet engine



Defination: 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”);





















1 comment: