An in-depth implementation of the Java servlet with Ajax proxies _java

Source: Internet
Author: User
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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.