Cross Context Dispatch

Hi Readers,

This blog is dedicated to cross context dispatch in J2EE.

There are 2 web applications:
1) ServletJSPAssignment8
2) ServletForwardRedirectTest

ServletJSPAssignment8 wants to forward the request to a resource in ServletForwardRedirectTest application. The resource in ServletForwardRedirectTest to be invoked is Forward2.jsp

Following are the code snippets:
1) Servlet in ServletJSPAssignment8 that gets the context of ServletForwardRedirectTest and forwards the request to Forward2.jsp in ServletForwardRedirectTest.

package com.badal.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FormServlet extends HttpServlet implements Servlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

ServletContext sc=null;

sc=getServletContext().getContext("/ServletForwardRedirectTest");
System.out.println("sc-->"+sc.toString());

RequestDispatcher rd=sc.getRequestDispatcher("/Forward2.jsp");
rd.forward(request, response);

}

}

2) We also need to modify the context.xml file of Tomcat. By default cross context dispatching is disabled. We have to modify the context tag of context.xml to enable cross context dispatching.
Make the following change in the context.xml file:

This should allow you to communicate between 2 web applications.
As rightly mentioned in the earlier posts by Bear, file sharing (adding Forward2.jsp in ServletJSPAssignment8 in this case) is a much more simpler option.
Cross context dispatching should only be done if you know exactly what you are doing.

Thanks for taking the time to read the blog.]

Advertisements

About Badal Chowdhary

I am a Software Engineer by profession. I have done SCJP and SCWCD certifications. Like working on cutting edge technologies and frameworks. Driven by challenges and fascinated by technology. I love playing and watching sports: Cricket, Ping Pong, Tennis, Badminton, Racket Ball and Gym.
This entry was posted in JSP & Servlets and tagged . Bookmark the permalink.

One Response to Cross Context Dispatch

  1. manoj says:

    is missing . Thanks for your post 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s