时间:2021-05-19
代码如下所示:
复制代码 代码如下:
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
*
* will return 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;
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
java仿Servlet生成验证码实例详解实现原理:使用BufferedImage对象的Graphics来进行绘制,然后输出成一张图片进行保存实现代码及详解:p
Servlet中操作文件详解及实例因为Servlet本来就是一个.Java文件,因此servlet中操作文件和普通java文件操作文件是一样的。读取文件主要代码
本文实例讲述了AJAX+Servlet实现的数据处理显示功能。分享给大家供大家参考,具体如下:实现功能:在输入框中输入字符,用AJAX传到后台Servlet处理
Java基础之Filter的实例详解定义:Filter,是Servlet的一种,接口类为javax.servlet.Filter,以一种模块化或者可重用的方法封
详解javaJDK动态代理类分析(java.lang.reflect.Proxy)/***JDK动态代理类分析(java.lang.reflect.Proxy使