深入Ajax代理的Java Servlet的实现详解
更新时间:2013年06月04日 11:53:51 作者:
本篇文章是对Ajax代理的Java Servlet的实现方法进行了详细的分析介绍,需要的朋友参考下
代码如下所示:
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;
}
}
复制代码 代码如下:
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;
}
}
相关文章
在Java中使用redisTemplate操作缓存的方法示例
这篇文章主要介绍了在Java中使用redisTemplate操作缓存的方法示例,在Redis中可以存储String、List、Set、Hash、Zset。感兴趣的可以了解一下2019-01-01Oracle + Mybatis实现批量插入、更新和删除示例代码
利用MyBatis动态SQL的特性,我们可以做一些批量的操作,下面这篇文章主要给大家介绍了关于Oracle + Mybatis实现批量插入、更新和删除的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。2018-01-01SpringMVC中的SimpleUrlHandlerMapping用法详解
这篇文章主要介绍了SpringMVC中的SimpleUrlHandlerMapping用法详解,SimpleUrlHandlerMapping是Spring MVC中适用性最强的Handler Mapping类,允许明确指定URL模式和Handler的映射关系,有两种方式声明SimpleUrlHandlerMapping,需要的朋友可以参考下2023-10-10Java8加java10等于Java18的版本查看及特性详解
这篇文章主要为大家介绍了Java 8加java10等于Java18的各个版本要点详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-06-06解决nacos项目启动报错:Connection refused: no further&
这篇文章主要介绍了解决nacos项目启动报错:Connection refused: no further informa问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-04-04
最新评论