Servlet to servlet connection across servers
We have a Servlet/JSP based application running on Websphere application server running on AIX server. There is another Tomcat server (on a different machine) between the client and previously mentioned application on Websphere server. How can i make a connection between Tomcat and websphere? What i have thought of is - deploy a servlet on Tomcat and make a servlet to servlet connection using java.net.URL & URL connec开发者_如何转开发tion. Which redirect the request coming on tomcat server from client to Websphere server application and get a response back in terms of byte stream.
what are the pros and cons of using such solution? What are other alternatives or better design options?
You could let the client send the request to the other server directly. So instead of
<form action="generatereport" method="post">
use
<form action="http://other.com/generatereport" method="post">
any extra parameters could be passed by <input type="hidden">
.
Or if supports GET, you could instead of method="get"
also just do
<a href="http://other.com/generatereport?param=foo">
or
response.sendRedirect("http://other.com/generatereport?param=foo");
Or if there are certain security restrictions and/or the client shouldn't know about the URL of the other server, then your best bet is indeed to play for proxy yourself with help of URLConnection
.
URLConnection connection = new URL("http://other.com/generatereport").openConnection();
// Copy necessary request headers from request.getHeader() to connection.setRequestProperty().
// If POST, copy request.getInputStream() to connection.getOutputStream() as well.
// Copy necessary response headers from connection.getHeaderField() to response.setHeader().
// Finally copy connection.getInputStream() to response.getOutputStream().
精彩评论