YahooSearchGatewayServlet.java
上传用户:shen332233
上传日期:2021-09-03
资源大小:7478k
文件大小:1k
源码类别:

Ajax

开发平台:

Java

  1. package ajaxbook.chap4;
  2. import java.io.*;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. public class YahooSearchGatewayServlet extends HttpServlet {
  8.     private static final String YAHOO_SEARCH_URL = 
  9.         "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=thunderboltsoftware"
  10.         + "&type=all";
  11.     
  12.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  13.     throws ServletException, IOException {
  14.         
  15.         String url = YAHOO_SEARCH_URL + "&" + request.getQueryString();
  16.         
  17.         HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
  18.         con.setDoInput(true);
  19.         con.setDoOutput(true);
  20.         
  21.         con.setRequestMethod("GET");
  22.         
  23.         //Send back the response to the browser
  24.         response.setStatus(con.getResponseCode());
  25.         response.setContentType("text/xml");
  26.         
  27.         BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  28.         String input = null;
  29.         OutputStream responseOutput = response.getOutputStream();
  30.         
  31.         while((input = reader.readLine()) != null) {
  32.             responseOutput.write(input.getBytes());
  33.         }
  34.     
  35.     }
  36.     
  37.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  38.     throws ServletException, IOException {
  39.         processRequest(request, response);
  40.     }
  41. }