aspose file tools*
The moose likes Servlets and the fly likes Forwarding to a different server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Forwarding to a different server" Watch "Forwarding to a different server" New topic
Author

Forwarding to a different server

Larry Cryderm
Greenhorn

Joined: Mar 23, 2004
Posts: 9
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.


I shoulda been a rock star.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Larry Cryderm
Greenhorn

Joined: Mar 23, 2004
Posts: 9
I thought of that, but didn't know if serialization was involved.
Do you have a recommendation?
Thanks.
Giri Kosuru
Greenhorn

Joined: Nov 13, 2001
Posts: 5
If you need to pass few parameters only to the server , try response.Redirect
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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

Joined: Aug 29, 2003
Posts: 155
Use an http client and remake the request.


SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="http://webtools.servehttp.com" target="_blank" rel="nofollow">http://webtools.servehttp.com</a> down for a while...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forwarding to a different server