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" --/><%
}
--%>
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
2. Without configuring each context.xml files, you can directly enable
$CATALINA_HOME/conf/[enginename]/[hostname]/ folder.
For more info regarding Tomcat's context container visit this link.
No comments:
Post a Comment