WEB开发网
开发学院WEB开发Jsp Servlet实现的代理服务器功能 阅读

Servlet实现的代理服务器功能

 2008-01-05 08:42:15 来源:WEB开发网   
核心提示:import java.io.*;import java.net.*;import javax.servlet.*;import javax.servlet.http.*;public class PRoxy extends HttpServlet{ public void doGet(HttpServletReque

  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);
}
}
}

Tags:Servlet 实现 代理

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接