The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          就是在前臺中調用proxy程序的servlet,設置參數servletName和其它參數。代理程序會將該請求發送到目的地址的名稱為servletName的servlet中去,并將其它參數作為請求的參數,在得到結果后,將內容原樣輸出到請求頁面。
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.io.UnsupportedEncodingException;
          import java.net.HttpURLConnection;
          import java.net.MalformedURLException;
          import java.net.URL;
          import java.net.URLEncoder;
          import java.util.Iterator;
          import java.util.Map;
          import java.util.Map.Entry;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          public class ProxyServlet extends HttpServlet {
          private String url;
          /**
          * 對servlet進行請求處理,并將結果在指定輸出流中輸出
          * @param os
          * @param servletName
          * @param parm
          * @throws IOException
          * @throws MalformedURLException
          */
          private void process(HttpServletRequest req, HttpServletResponse resp,
          String[] target) throws MalformedURLException, IOException {
          // 取得連接
          HttpURLConnection huc = (HttpURLConnection) new URL(url + target[0])
          .openConnection();
          // 設置連接屬性
          huc.setDoOutput(true);
          huc.setRequestMethod("POST");
          huc.setUseCaches(false);
          huc.setInstanceFollowRedirects(true);
          huc.setRequestProperty("Content-Type",
          "application/x-www-form-urlencoded");
          huc.connect();
          // 往目標servlet中提供參數
          OutputStream targetOS = huc.getOutputStream();
          targetOS.write(target[1].getBytes());
          targetOS.flush();
          targetOS.close();
          // 取得頁面輸出,并設置頁面編碼及緩存設置
          resp.setContentType(huc.getContentType());
          resp.setHeader("Cache-Control", huc.getHeaderField("Cache-Control"));
          resp.setHeader("Pragma", huc.getHeaderField("Pragma"));
          resp.setHeader("Expires", huc.getHeaderField("Expires"));
          OutputStream os = resp.getOutputStream();
          // 將目標servlet的輸入流直接往頁面輸出
          InputStream targetIS = huc.getInputStream();
          int r;
          while ((r = targetIS.read()) != -1) {
          os.write(r);
          }
          targetIS.close();
          os.flush();
          os.close();
          huc.disconnect();
          }
          /**
          * 將參數中的目標分離成由目標servlet名稱和參數組成的數組
          * @param queryString
          * @return
          * @throws UnsupportedEncodingException
          */
          private String[] parse(Map map) throws UnsupportedEncodingException {
          String[] arr = { "", "" };
          Iterator iter = map.entrySet().iterator();
          while (iter.hasNext()) {
          Map.Entry me = (Entry) iter.next();
          String[] varr = (String[]) me.getValue();
          if ("servletName".equals(me.getKey())) {
          // 取出servlet名稱
          arr[0] = varr[0];
          } else {
          // 重新組裝參數字符串
          for (int i = 0; i < varr.length; i++) {
          // 參數需要進行轉碼,實現字符集的統一
          arr[1] += "&" + me.getKey() + "="
          + URLEncoder.encode(varr[i], "utf-8");
          }
          }
          }
          arr[1] = arr[1].replaceAll("^&", "");
          return arr;
          }
          @Override
          public void init() throws ServletException {
          // 設置目標服務器地址
          url = this.getInitParameter("url");
          if (!url.endsWith("/"))
          url = url + "/";
          }
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
          this.doPost(req, resp);
          }
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
          String[] target = parse(req.getParameterMap());
          process(req, resp, target);
          }
          }
          posted on 2015-01-26 17:32 Eric_jiang 閱讀(258) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 青岛市| 鸡西市| 双流县| 清远市| 威宁| 汤阴县| 常山县| 门源| 小金县| 连江县| 酒泉市| 桑植县| 城市| 无棣县| 高陵县| 阳原县| 武功县| 于都县| 泗水县| 阿瓦提县| 云龙县| 攀枝花市| 新巴尔虎右旗| 阳朔县| 高安市| 佳木斯市| 万荣县| 同江市| 汪清县| 临安市| 浦东新区| 临沭县| 鲁山县| 武义县| 吴旗县| 泰和县| 元江| 高要市| 海伦市| 宣威市| 太白县|