The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          就是在前臺(tái)中調(diào)用proxy程序的servlet,設(shè)置參數(shù)servletName和其它參數(shù)。代理程序會(huì)將該請(qǐng)求發(fā)送到目的地址的名稱為servletName的servlet中去,并將其它參數(shù)作為請(qǐng)求的參數(shù),在得到結(jié)果后,將內(nèi)容原樣輸出到請(qǐng)求頁面。
          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;
          /**
          * 對(duì)servlet進(jìn)行請(qǐng)求處理,并將結(jié)果在指定輸出流中輸出
          * @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();
          // 設(shè)置連接屬性
          huc.setDoOutput(true);
          huc.setRequestMethod("POST");
          huc.setUseCaches(false);
          huc.setInstanceFollowRedirects(true);
          huc.setRequestProperty("Content-Type",
          "application/x-www-form-urlencoded");
          huc.connect();
          // 往目標(biāo)servlet中提供參數(shù)
          OutputStream targetOS = huc.getOutputStream();
          targetOS.write(target[1].getBytes());
          targetOS.flush();
          targetOS.close();
          // 取得頁面輸出,并設(shè)置頁面編碼及緩存設(shè)置
          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();
          // 將目標(biāo)servlet的輸入流直接往頁面輸出
          InputStream targetIS = huc.getInputStream();
          int r;
          while ((r = targetIS.read()) != -1) {
          os.write(r);
          }
          targetIS.close();
          os.flush();
          os.close();
          huc.disconnect();
          }
          /**
          * 將參數(shù)中的目標(biāo)分離成由目標(biāo)servlet名稱和參數(shù)組成的數(shù)組
          * @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 {
          // 重新組裝參數(shù)字符串
          for (int i = 0; i < varr.length; i++) {
          // 參數(shù)需要進(jìn)行轉(zhuǎn)碼,實(shí)現(xiàn)字符集的統(tǒng)一
          arr[1] += "&" + me.getKey() + "="
          + URLEncoder.encode(varr[i], "utf-8");
          }
          }
          }
          arr[1] = arr[1].replaceAll("^&", "");
          return arr;
          }
          @Override
          public void init() throws ServletException {
          // 設(shè)置目標(biāo)服務(wù)器地址
          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 閱讀(257) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 闵行区| 定南县| 绥中县| 栾城县| 环江| 五华县| 应用必备| 杭锦后旗| 石阡县| 咸丰县| 阳西县| 石林| 抚顺市| 聊城市| 连州市| 溆浦县| 绥德县| 元氏县| 台南市| 武定县| 巩义市| 昌江| 珠海市| 舟山市| 蓬莱市| 建德市| 和硕县| 镇巴县| 尉氏县| 布尔津县| 中牟县| 黔江区| 沿河| 鄂尔多斯市| 乌拉特中旗| 唐山市| 西丰县| 珲春市| 门源| 交口县| 迁西县|