File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Communication between servlets of two different applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Communication between servlets of two different applications" Watch "Communication between servlets of two different applications" New topic
Author

Communication between servlets of two different applications

Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Ranchers,

I have a question regarding invoking a resource from other application.

Consider, there are 2 web applications viz., WebApplication1 and WebApplication2.

WebApplication1 has Servlet1 and WebApplication2 has Servlet2.

How can we forward the request to Servlet2 (in WebApplication2) from Servlet1 (in WebApplication1).

In Servlet1 we can write,


I think we have to play with ServletContext. Somehow we have to get the ServletContext of WebApplication2 in WebApplication1.

Any ideas are welcome.

Thanks for your time and consideration.

Best Regards
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

You can Do that by using RMI.
Good luck


Java Best Practices
Linux Best Practices
Amortization Calculator

Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Vinoth,

Thanks for the reply.

I am not sure if it has to be so complicated.

There should be way to achieve this with out RMI.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

First you need to consider why you are doing this. Whenever I think this is necessary, I usually figure out that the code sharing is more appropriate at build time rather than run time. That may not be the case in your situation, but it's something to carefully consider before diving off.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

If it is a same server there is no need to use rmi we can do it by RequestDispatcher but if it is a different server then we need to use RMI.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

There is no need for something as low-level as RMI.

If it turns out that run-time sharing is appropriate (again, think seriously on that), and they are in the same servlet container, and the container supports cross-context dispatching...

Or, an HTTP request can be issued to the other app...

Or, a web service interface could be established...
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Bear,

Its true that this is not a requirement in my project.

I can easily copy the servlet in WebApplication2 and paste it in WebApplication1. It will surely solve the problem (though there will be slight code duplication). This I guess is compile-time sharing.

Can you throw some more light on run time sharing of resources?

Is it possible to get the context of WebApplication2 in WebApplication1?

Thanks for your valuable time.

Best Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

While it's not something I've ever done myself (as I said, I use compile time code sharing whenever possible), Tomcat will allow cross-context dispatching if it is configured for such.

For more specifics, you might want to search through the Tomcat forum, where I know this has been discussed before, and the Tomcat documentation.
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Bear,

Thanks for your kind direction.

Best Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Moved to the Tomcat forum to discuss cross-context dispatching.
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi,

I could make the cross context dispatching work.

Following is the code:

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.



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 solution.

Best Regards
Anil Verma
Greenhorn

Joined: Mar 14, 2011
Posts: 1
Hi Guys, This forum has been pretty useful. But I wanted to ask if cross context dispatching will still work if one Web app is deployed on Tomcat and the other web app on WebSphere Application Server. And if not, what is the solution, if it is through RMI, could you give me few pointers on that. Thanks in Advance,
Baswanth Rao
Greenhorn

Joined: Nov 14, 2011
Posts: 2
Hi Anil,

Have you got any solution for this problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Communication between servlets of two different applications