Sunday, April 10, 2011

Inter-Servlet communication among different contexts

Suppose you have Servlet X in Web App A and Servlet Y in Web App B. You want to pass some parameters through your Web App A's JSP to your Servlet X and there that Servlet should pass those parameters again to another Servlet in difference context (i.e Web App B's Servlet Y) and then redirect back to the calling JSP in Web App A. Users may feel that they are dealing with single context but literally it will be tow difference contexts.

I will direct you to experience this by 5 steps.

1. Viewer JSP - index.jsp (User will simply click a button to pass parameters to a Servlet within the same context)

< Ex: input type="button" value="Fetch Info" name="Get_Info" onclick="location.href='ServletX?action=getParticipants&id=user&pwd=yawl'"/>

Here after you clicking the button "Fetch Info" it will call "ServletX" Servlet in the same Web App (same context) along with "getParticipants" and two other parameters.

Scenario discussed here is retrieving some info resides in another context to a jsp by invoking two Servlets.


2. Servlet X will collect parameters in its service method

String action = request.getParameter("action");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");

Then We can redirect it to another Servlet as below,

request.setAttribute("output", action );
ServletContext cont = getServletContext().getContext("/WebAppB");
RequestDispatcher dis = cont.getRequestDispatcher("/ServletY");
dis.forward( request, response );

//getServletContext().getContext("/WebAppB"); is the important part because in this way you can jump in to other contexts and by invoking the method getRequestDispatcher() you can forward messages to what ever jsp/servlet combinations as wish. But if you use getRequestDispatcher(String path) of the ServletRequest interface it cannot extend outside the current servlet context.

For more info visit the api for ServletContext.

3. Then Servlet Y in Web App B can access the parameters and apply what ever the logic has to be performed.

String action = (String) request.getAttribute("output");

//Business logic goes here

request.setAttribute("output", action);
ServletContext cont = getServletContext().getContext("/WebAppA");
RequestDispatcher dis = cont.getRequestDispatcher("/index.jsp");
dis.forward(request, response);

Here after applying what ever the logic you wants, you can switch back to WebApp A servlet's context by invoking getServletContext().getContext("/WebAppA");. Then you can redirect output variable to index jsp in WebApp A's context to view the results.

4. Therefore to receive and view the results you need to implement a code similar to this in your earlier index.jsp page.

<%--

if (request.getAttribute("output") != null) {

String suc = (String) request.getAttribute("output");
%><--input type="text" name="output" value="<%=suc%>" readonly="readonly" disabled="disabled" --/><%

}


--%>

5. Last step is very important because without setting crossContext parameter to true server will not allow to happen inter-servlet communication in two deference contexts due to security policies.

There are several ways to do this. First one is enabling in each web app's perspective and the second one is enabling in server side which affect globally for all web apps hosted.

1. In this way you have to set
crossContext parameter to true in each and every Web App's context.xml file which resides in META-INF folder of your web directory.

2. Without configuring each context.xml files, you can directly enable
crossContext parameter to true which resides in context.xml file in server's (Here it is Apache Tomcat) conf directory. Then in deployment time server will create context.xml files for each and every web apps in its host and enable crossContext parameter to true in each. These individual contexts files are located at $CATALINA_HOME/conf/[enginename]/[hostname]/ folder.

For more info regarding Tomcat's context container visit this link.
This is just a one scenario. But this can be applied in various ways to achieve communication among deference contexts in same Web Container.

No comments: