深入Ajax代理的Java Servlet的實現(xiàn)詳解
更新時間:2013年06月04日 11:53:51 作者:
本篇文章是對Ajax代理的Java Servlet的實現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
代碼如下所示:
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;
}
}
復(fù)制代碼 代碼如下:
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;
}
}
您可能感興趣的文章:
- jQuery+datatables插件實現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能示例
- MVC+jQuery.Ajax異步實現(xiàn)增刪改查和分頁
- jQuery的Ajax接收java返回數(shù)據(jù)方法
- ajax提交到j(luò)ava后臺之后處理數(shù)據(jù)的實現(xiàn)
- ajax java 實現(xiàn)自動完成功能
- AJAX+JAVA用戶登陸注冊驗證的實現(xiàn)代碼
- 詳解Java Ajax jsonp 跨域請求
- Java使用Ajax實現(xiàn)跨域上傳圖片功能
- Javaweb使用cors完成跨域ajax數(shù)據(jù)交互
- 在Java的Struts中判斷是否調(diào)用AJAX及用攔截器對其優(yōu)化
- AJAX實現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺】
相關(guān)文章
Spring?Boot開發(fā)RESTful接口與http協(xié)議狀態(tài)表述
這篇文章主要為大家介紹了Spring?Boot開發(fā)RESTful接口與http協(xié)議狀態(tài)表述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
springcloud連接遠(yuǎn)程nacos失敗顯示localhost服務(wù)連接失敗的問題解決
這篇文章主要介紹了springcloud連接遠(yuǎn)程nacos失敗顯示localhost服務(wù)連接失敗的問題解決,文中有詳細(xì)的代碼示例供大家參考,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-03-03
MyBatis實現(xiàn)自定義MyBatis插件的流程詳解
MyBatis的一個重要的特點就是插件機(jī)制,使得MyBatis的具備較強(qiáng)的擴(kuò)展性,我們可以根據(jù)MyBatis的插件機(jī)制實現(xiàn)自己的個性化業(yè)務(wù)需求,本文給大家介紹了MyBatis實現(xiàn)自定義MyBatis插件的流程,需要的朋友可以參考下2024-12-12
利用feign調(diào)用返回object類型轉(zhuǎn)換成實體
這篇文章主要介紹了利用feign調(diào)用返回object類型轉(zhuǎn)換成實體,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
spring cloud服務(wù)之間的調(diào)用之ribbon詳解
關(guān)于spring-cloud的服務(wù)調(diào)用,我們首先需要了解它的兩個核心組件Ribbon和Feign。接下來通過本文給大家詳細(xì)介紹spring-cloud服務(wù)之間的調(diào)用之ribbon,感興趣的朋友一起看看吧2021-08-08
使用SpringBoot實現(xiàn)Redis多數(shù)據(jù)庫緩存
在我的系統(tǒng)中,為了優(yōu)化用戶行為數(shù)據(jù)的存儲與訪問效率,我引入了Redis緩存,并將數(shù)據(jù)分布在不同的Redis數(shù)據(jù)庫中,通過這種方式,可以減少單一數(shù)據(jù)庫的負(fù)載,提高系統(tǒng)的整體性能,所以本文給大家介紹了使用SpringBoot實現(xiàn)Redis多數(shù)據(jù)庫緩存,需要的朋友可以參考下2024-06-06

