The code looks like this:
Copy Code code as follows:
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URL;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/**
* Take Any request and proxy it to the given redirect_base.
* For example, if this servlet lives at
*
* Http://foo.com/forward
*
* and is inititialized with the Redirect_base
*
* Http://bar.com/some/path
*
* Then a GET request like
*
* Http://foo.com/forward?quux=mumbley
*
* 'll return to the results of a get from
*
* Http://bar.com/some/path?quux=mumbley
*
* This is not robust and generalized; It ' s simple and quick.
*
* @author JDF
*
*/
public class Proxyservlet extends HttpServlet
{
Private final static String COPYRIGHT = Com.ibm.dogear.Copyright.SHORT;
public static final String redirect_base = "Com.ibm.bl.servlet.RedirectServlet.redirect_base";
Private String redirectbase;
@Override
public void init (ServletConfig config) throws Servletexception
{
Super.init (config);
Redirectbase = Getrequiredparam (redirect_base);
}
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp) throws IOException
{
String querystring = req.getquerystring ();
URL url = new URL (redirectbase + querystring!= null?) "?" + QueryString: ""));
Copyinputstreamtooutputstream (Url.openstream (), Resp.getoutputstream ());
}
private void Copyinputstreamtooutputstream (InputStream in, servletoutputstream out)
Throws IOException
{
Try
{
Try
{
byte[] buffer = new byte[1024];
int n;
while ((n = in.read (buffer))!=-1)
Out.write (buffer, 0, N);
}
Finally
{
Out.close ();
}
}
Finally
{
In.close ();
}
}
Protected string Getrequiredparam (string param) throws Servletexception
{
String result = Getservletconfig (). Getinitparameter (param);
if (result = = null) {
throw new Servletexception (GetClass () + "requires" + param + "param");
}
return result;
}
}