程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> J2EE基礎:Servlet實現代理服務器功能

J2EE基礎:Servlet實現代理服務器功能

編輯:關於JAVA

J2EE基礎:Servlet實現代理服務器功能。

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
  public class Proxy extends HttpServlet
{ public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{ String query = null;
  response.setContentType("text/html");
PrintWriter out = response.getWriter();
  query = request.getParameter("URL");
if (query == null)
{ response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Missing URL parameter");
return;
}
  try
{ query = URLDecoder.decode(query);
}
catch(Exception exception)
{ response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"URL decode error " + exception);
return;
}
  try
{ URL url = new URL(query);
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
  String line;
while ((line = in.readLine()) != null)
out.println(line);
out.flush();
}
catch(IOException exception)
{ response.sendError(HttpServletResponse.SC_NOT_FOUND,
"Exception: " + exception);
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved