• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inter Servlet Communication

 
Samir Chaudhari
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Can 2 servlets residing on different servers communicate with each other & How
2)IS this scenario feasible & how
1)Login on Server1
2)Submit form to Servlet1 on Server1
3)Servlet1 does sendRedirect() the POST to servlet2 on Server2
4)Can teh session object from Server1 be forwarded to Server2.
Please help
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
U can not maintain the session when u invoke the servlet on a different Server.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, actually you can if the Application server is set up as a cluster as is possible with WebLogic (and others). This usually happen invisibly to the client tho so they can't tell which app server they are being serviced by.
The other side to this is that the servlets are usually wrapped up as a web archive (war file) and the applications are usually the same on both machines (excuse the broad generalisations, it is possible to split specific code to one machine etc but generally not)
The exception is when the servlets are in completely different applications/locations/domains etc. In this case the session would mean nothing to the second app server since it doesnt know anything about that particular session.
But I ramble.
Dave.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Samir Chaudhari:
1)Can 2 servlets residing on different servers communicate with each other & How

No, normally not unless they are part of a cluster (and then you generally wouldn't worry as a developer what runs on which server anyway).
1)Login on Server1
2)Submit form to Servlet1 on Server1
3)Servlet1 does sendRedirect() the POST to servlet2 on Server2

Beware of side effects. From the HTTP 1.1 specification: "If the 307 [Temporary Redirect -- PdH] status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued."
4)Can teh session object from Server1 be forwarded to Server2.
Not easily. What you'd do - if you have convinced yourself that you really must do this - is to wrap all the important state in a single Java object, then serialize it into a database to be picked up by the other server.
If you don't have a database, you could perhaps serialize and encrypt it, encode it (you could use java.net.URLEncoder/Decoder if you've got nothing better) and stick it in a hidden field on the form. On the other server you can decode, decrypt, and deserialize it. Not pretty, but it probably beats trying to set up any kind of network communication from scratch.
- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!