Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Communication between servlets of two different applications

 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can Do that by using RMI.
Good luck
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinoth Thirunavukarasu
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Thanks for your kind direction.

Best Regards
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Tomcat forum to discuss cross-context dispatching.
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anil,

Have you got any solution for this problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic