servlet中的HttpServletRequest,HttpServletResponse對象以及service 和 doXX方法區別

來源:互聯網
上載者:User

標籤:

一:HttpServletRequest對象作用是用於擷取請求資料。

 核心的API:                    請求行:                         request.getMethod();   請求方式                        request.getRequetURI()   / request.getRequetURL()   請求資源                        request.getProtocol()   請求http協議版本                                        要求標頭:                        request.getHeader("名稱")   根據要求標頭擷取請求值                        request.getHeaderNames()    擷取所有的要求標頭名稱                    實體內容:                        request.getInputStream()   擷取實體內容資料

二:service 和 doXX方法區別

HttpSevlet類的源碼:protected void service(HttpServletRequest req, HttpServletResponse resp)        throws ServletException, IOException {       //得到請求方式        String method = req.getMethod();        if (method.equals(METHOD_GET)) {            long lastModified = getLastModified(req);            if (lastModified == -1) {                // servlet doesn‘t support if-modified-since, no reason                // to go through further expensive logic                doGet(req, resp);            } else {                long ifModifiedSince;                try {                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);                } catch (IllegalArgumentException iae) {                    // Invalid date header - proceed as if none was set                    ifModifiedSince = -1;                }                if (ifModifiedSince < (lastModified / 1000 * 1000)) {                    // If the servlet mod time is later, call doGet()                    // Round down to the nearest second for a proper compare                    // A ifModifiedSince of -1 will always be less                    maybeSetLastModified(resp, lastModified);                    doGet(req, resp);                } else {                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);                }            }        } else if (method.equals(METHOD_HEAD)) {            long lastModified = getLastModified(req);            maybeSetLastModified(resp, lastModified);            doHead(req, resp);        } else if (method.equals(METHOD_POST)) {            doPost(req, resp);                    } else if (method.equals(METHOD_PUT)) {            doPut(req, resp);                            } else if (method.equals(METHOD_DELETE)) {            doDelete(req, resp);                    } else if (method.equals(METHOD_OPTIONS)) {            doOptions(req,resp);                    } else if (method.equals(METHOD_TRACE)) {            doTrace(req,resp);                    } else {            //            // Note that this means NO servlet supports whatever            // method was requested, anywhere on this server.            //            String errMsg = lStrings.getString("http.method_not_implemented");            Object[] errArgs = new Object[1];            errArgs[0] = method;            errMsg = MessageFormat.format(errMsg, errArgs);                        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);        }    }

三:傳遞的請求參數擷取
  GET方式: 參數放在URI後面
  POST方式: 參數放在實體內容中

    擷取GET方式參數:
      request.getQueryString();
    擷取POST方式參數:
      request.getInputStream();

四:問題:但是以上兩種不通用,而且擷取到的參數還需要進一步地解析。
所以可以使用統一方便的擷取參數的方式:

核心的API:
  request.getParameter("參數名"); 根據參數名擷取參數值(注意,只能擷取一個值的參數)
  request.getParameterValue("參數名“);根據參數名擷取參數值(可以擷取多個值的參數)

  request.getParameterNames(); 擷取所有參數名稱列表

五:請求參數編碼問題
  修改POST方式參數編碼:
    request.setCharacterEncoding("utf-8");
  修改GET方式參數編碼:
    手動解碼:String name = new String(name.getBytes("iso-8859-1"),"utf-8");

六:響應行
  #http協議版本
  #狀態代碼: 伺服器處理請求的結果(狀態)
  常見的狀態:
    200 : 表示請求處理完成並完美返回
    302: 表示請求需要進一步細化。
    404: 表示客戶訪問的資源找不到。
    500: 表示伺服器的資源發送錯誤。(伺服器內部錯誤)
七:常見的回應標頭

Location: http://www.it315.org/index.jsp   -表示重新導向的地址,該頭和302的狀態代碼一起使用。Server:apache tomcat                 ---表示伺服器的類型Content-Encoding: gzip                 -- 表示伺服器發送給瀏覽器的資料壓縮類型Content-Length: 80                    --表示伺服器發送給瀏覽器的資料長度Content-Language: zh-cn               --表示伺服器支援的語言Content-Type: text/html; charset=GB2312   --表示伺服器發送給瀏覽器的資料類型及內容編碼Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT  --表示伺服器資源的最後修改時間Refresh: 1;url=http://www.it315.org     --表示定時重新整理Content-Disposition: attachment; filename=aaa.zip --表示告訴瀏覽器以下載方式開啟資源(下載檔案時用到)Transfer-Encoding: chunkedSet-Cookie:SS=Q0=5Lb_nQ; path=/search   --表示伺服器發送給瀏覽器的cookie資訊(會話管理用到)Expires: -1                           --表示通知瀏覽器不進行緩衝Cache-Control: no-cachePragma: no-cacheConnection: close/Keep-Alive           --表示伺服器和瀏覽器的串連狀態。close:關閉串連 keep-alive:儲存串連

HttpServletResponse:

HttpServletResponse對象修改響應資訊:

  響應行:
    response.setStatus() 設定狀態代碼
  回應標頭:
    response.setHeader("name","value") 設定回應標頭
  實體內容:
    response.getWriter().writer(); 發送字元實體內容
    response.getOutputStream().writer() 發送位元組實體內容

 

servlet中的HttpServletRequest,HttpServletResponse對象以及service 和 doXX方法區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.