Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forwarding to a different server

 
Larry Cryderm
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anyone successfully forwarded a request and response to a servlet residing on a different server?

We have multiple servers and want to forward some requests to a specific server. All servers run Servlets.

I thought this would work
request.getRequestDispatcher("urlToOtherServer").forward(request, response);
...but what I read is that this will only work on the same server.

I didn't want to open a URL and URLConnection to do this.

Any help or ideas appreciated.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think for a moment. You're forwarding REFERENCES TO the request and response Objects.
On the other server these don't exist so the forward won't work.
 
Larry Cryderm
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought of that, but didn't know if serialization was involved.
Do you have a recommendation?
Thanks.
 
Giri Kosuru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to pass few parameters only to the server , try response.Redirect
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Larry Cryderm:
I thought of that, but didn't know if serialization was involved.
Do you have a recommendation?
Thanks.


You can't serialize objects over a network during a forward.
You'd need a special mechanism you will have to write yourself to handle it, and send all the data before you do the forward, then in the receiving servlet read in that information based on whatever (maybe use a fixed name, but that's obviously risky as you can't handle more than one request that way).

No, it is not going to be pretty. Better just create a new URL and do a POST request to the other machine making sure all relevant data from the session and request are turned into HTML parameters (meaning essentially plain text in the HTTP headers).
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an http client and remake the request.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic