Practical example of inter application forwarding

In this practical example of inter application forwarding, we will create App1 and App2 applications which were discussed in the last post. Here we go, first one is the single line home page (index.html) of App1 :


S1 servlet of App1

Second, S1 servlet of App1 :

package com.techmentro.learningpad;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class ServletOne extends HttpServlet 
{
public void doGet(HttpServletRequest request,
		HttpServletResponse response) 
				throws ServletException,IOException
{
//ServletContext of the current app is obtained from the ServletConfig.
ServletContext ctx=getServletConfig().getServletContext();
//ServletContext of App2 is requested from the ServletContext of App1.
ServletContext ctxOfApp2=ctx.getContext("/App2");
if(ctxOfApp2!=null)
{
//RequestDispatcher for S2 servlet is obtained from the ServletContext of App2.
//It is assumed that S2 servlet has /servletTwo uri in App2.
RequestDispatcher rd=
ctxOfApp2.getRequestDispatcher("/servletTwo");
//Request is forwarded to S2 servlet.
rd.forward(request,response);
}
else
{
	//Error Message is sent as response to the client.
	PrintWriter out=response.getWriter();
	out.println("<b>Either App2 isn't deployed or the server isn't configured for cross context calls.</b>");
	out.close();
}
}
}

Third, the web.xml of App1 :


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
    <servlet-name>s1</servlet-name>
    <servlet-class>com.techmentro.learningpad.ServletOne</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>s1</servlet-name>
    <url-pattern>/servletOne</url-pattern>
  </servlet-mapping>
</web-app>

Fourth, the S2 servlet of App2 :

package com.techmentro.learningpad;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletTwo extends HttpServlet
{
	public void doGet(
			HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.println("<b>This output is generated by servletTwo of app2.</b>");
		out.close();
	}

}

Finally, the web.xml of App2 :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  
  
  <servlet>
    <servlet-name>s2</servlet-name>
    <servlet-class>com.techmentro.learningpad.ServletTwo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>s2</servlet-name>
    <url-pattern>/servletTwo</url-pattern>
  </servlet-mapping>
  
</web-app>

Before testing this practical example of inter application forwarding, make sure that crossContext attribute is set to true in context.xml file of Tomcat as described in the last post. In case of other servers, read server documentation to find out how to enable inter application forwarding.


If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

2 thoughts on “Practical example of inter application forwarding

  1. Sir, when i did interapplication forwarding it said app isn’t deployed or server is not confg still i had set crossContext attribute to true in context.xml, pls tell me what is wrong in the process.

Leave a Reply

Your email address will not be published. Required fields are marked *