Bookmark Topic Watch Topic
  • New Topic

communication between servlets on different hosts  RSS feed

 
mi go
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
can we have communication between two servlets that are hosted on differerent host ? For example i have servlet1 on localhost and servlet2 on different localhost machine can they pass some data to each other and have some sort of communication?
 
Tim Moores
Saloon Keeper
Posts: 3570
77
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
The easiest way would probably be to have them communicate via HTTP, since they're already set up as web apps. The built-in java.net.URL class (and its friends URLConnection etc.) can be used for that, or for fancier stuff something like the Apache HttpComponents library.
 
Santhosh ayiappan
Ranch Hand
Posts: 81
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
As mentioned by Tim, there are lot of ways to invoke Servlets running on different URL. Apache HTTP Client provides different options to invoke the servlet.

Here is the sample program to invoke a servlet
 
mi go
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
Actually i am confused...i want to realize a system in such way that two web application resideing on different hosts (different machines )to pass some data to each other and communicate but in this case how will it be realised? Like i have one client-server (webapp1) on one machine and other client server(webapp 2) on another machine?
Or is it like client on one machine and server on another machine?
Can someone guide me on this?
 
Tim Moores
Saloon Keeper
Posts: 3570
77
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
In this scenario, one web app would be client to the other web app (which would act as the server). And vice versa, if both web apps call each other.
 
mi go
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
so we have to write client+server code a both the places.  Can this be realised in using servlet and tomcat server ? Can you provide some examples or source code for it?
 
Tim Moores
Saloon Keeper
Posts: 3570
77
  • Mark post as helpful
  • send pies
  • Report post to moderator
The client would simply be Java code that makes network calls, and it would be part of each web app. In my first post I mentioned to ways to implement that. If you search for "java http client example" or some such phrase you'll find plenty of example code.
 
mi go
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
So if i setup a VM on my laptop running windows then will it be treated as different host ?
if yes then i want to have communication between two apps/servlets, one in VM and one in my laptop....also it is confusing the difference between apache tomcat and apache http server.....can you explain what exactly HTTP client needs?
 
Tim Moores
Saloon Keeper
Posts: 3570
77
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
So if i setup a VM on my laptop running windows then will it be treated as different host ?

Yes. You don't need a VM, though - you can run two Tomcat instances on the same machine, as long as they use different ports. Or, even easier, you can create two web apps running within the same Tomcat

it is confusing the difference between apache tomcat and apache http server

Tomcat is a servlet/JSP container written in Java. Httpd is a web server written in C that can be used as a front for Tomcat, providing capabilities that Tomcat does not have itself.

can you explain what exactly HTTP client needs?

I'm not sure what you're asking; can you rephrase the question, while giving more detail?
 
mi go
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
Actually i am not understanding the server part in servlet technology. Consider following scenario that i currently have.

I have windows system on which i have installed apache tomcat and consists of two servlets namely Servlet1 and Sevlet2. Servlet1 passes one parameter to servlet 2 so when i go to url of servlet2 on localhost, it shows that parameter. Now i have to seperate servlet1 and servlet2 say servlet1 reamains on host OS and servlet2 on windowsVM and then have same type of communication between them. What things i have to do with windowsVM like install eclipse,tomcat etc?  Do we need two tomcat servers on both the hosts i.e one on VM and one on Host OS ?
 
Ron McLeod
Saloon Keeper
Posts: 1459
209
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Report post to moderator
mi go wrote:... Servlet1 passes one parameter to servlet 2 so when i go to url of servlet2 on localhost, it shows that parameter...

It's unclear (to me anyway) what you are wanting.

It sounds like you want to be able to share data between two servlets, regardless of whether they are being served by the same Tomcat instance or not, and regardless of whether they are running on different computing platforms (physical or virtual).  You are not actually looking for a mechanism to communicate between servlets.

Is that correct?
 
mi go
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
to get straight i am looking for mechanism that allows two servlet to communicate with each other residing on differernt hosts i.e. one on host OS(host 1) and one on VM(host 2). I am corncerned about environment setup(tomcat) and the code for the servlet. 
 
Ron McLeod
Saloon Keeper
Posts: 1459
209
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Report post to moderator
mi go wrote:... i am looking for mechanism that allows two servlet to communicate with each other residing on differernt hosts

Ok, but can you expand on what purpose the communications is for?

From this statement:
mi go wrote:... Servlet1 passes one parameter to servlet 2 so when i go to url of servlet2 on localhost, it shows that parameter...

it sounds like you want to collect information from the browser in one servlet and use that information another servlet.



Will it be the same browser visiting the two URLs?

Will the browser independently visit the two URLs, or will the first servlet redirect the browser to the second URL?
 
mi go
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
Communication is just like that i.e passing of parameters or two servlets saying Hi to each other simply(chatting app)


It is not picking up anything from browser.

servlet 1 code:



servlet2 code:


 
Ron McLeod
Saloon Keeper
Posts: 1459
209
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Report post to moderator
Continued in this thread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!