1. 程式人生 > >doPost()詳解

doPost()詳解

 此方法中註釋的意思為:

由伺服器(通過<code>service</code>方法)呼叫,以允許servlet處理POST請求。

HTTP POST方法允許客戶端一次向Web伺服器傳送無限長的資料,並且在釋出諸如信用卡號碼之類的資訊時非常有用。

<p>當重寫此方法時,讀取請求資料,寫入響應頭,獲取響應的寫入器或輸出流物件,最後寫入響應資料。最好包括內容型別和編碼。當使用<code>PrintWriter</code>物件返回響應時,在訪問<code>PrintWriter</code>物件之前設定內容型別。

Servlet容器在提交響應之前必須寫入報頭,因為在HTTP中,報頭必須在響應主體之前傳送。如果整個響應都在響應緩衝區中,則自動設定內容長度。

如果可能,設定Content-Length頭部(使用{@link javax.servlet.ServletResponse#setContentLength}方法),

允許servlet容器使用持久連線將響應返回給客戶端,從而提高效能。如果整個響應都位於響應緩衝區中,則自動設定內容長度。

當使用HTTP 1.1分塊編碼(這意味著響應具有Transfer-Encoding報頭)時,不要設定Content-Length報頭。

此方法不需要是安全的或冪等的。通過POST請求的操作可能具有副作用,使用者可以對此負責,例如,更新儲存的資料或線上購買物品。

博主補充:這就是常說的post方法提交表單時,如果重新整理當前頁面會重複提交的問題,這句話想表達的就是這個意思;什麼是冪等呢?

冪等(idempotent、idempotence)是一個數學與計算機學概念,常見於抽象代數中。

在程式設計中一個冪等操作的特點是其任意多次執行所產生的影響均與一次執行的影響相同。---摘自百度百科

<p>如果HTTP POST請求的格式不正確,

<code>doPost</code>返回HTTP“Bad Request”訊息。

 

 

    /**
     * Called by the server (via the <code>service</code> method)
     * to allow a servlet to handle a POST request.
     *
     * The HTTP POST method allows the client to send
     * data of unlimited length to the Web server a single time
     * and is useful when posting information such as
     * credit card numbers.
     *
     * <p>When overriding this method, read the request data,
     * write the response headers, get the response's writer or output
     * stream object, and finally, write the response data. It's best
     * to include content type and encoding. When using a
     * <code>PrintWriter</code> object to return the response, set the
     * content type before accessing the <code>PrintWriter</code> object.
     *
     * <p>The servlet container must write the headers before committing the
     * response, because in HTTP the headers must be sent before the
     * response body.
     *
     * <p>Where possible, set the Content-Length header (with the
     * {@link javax.servlet.ServletResponse#setContentLength} method),
     * to allow the servlet container to use a persistent connection
     * to return its response to the client, improving performance.
     * The content length is automatically set if the entire response fits
     * inside the response buffer.
     *
     * <p>When using HTTP 1.1 chunked encoding (which means that the response
     * has a Transfer-Encoding header), do not set the Content-Length header.
     *
     * <p>This method does not need to be either safe or idempotent.
     * Operations requested through POST can have side effects for
     * which the user can be held accountable, for example,
     * updating stored data or buying items online.
     *
     * <p>If the HTTP POST request is incorrectly formatted,
     * <code>doPost</code> returns an HTTP "Bad Request" message.
     *
     *
     * @param req   an {@link HttpServletRequest} object that
     *                  contains the request the client has made
     *                  of the servlet
     *
     * @param resp  an {@link HttpServletResponse} object that
     *                  contains the response the servlet sends
     *                  to the client
     *
     * @exception IOException   if an input or output error is
     *                              detected when the servlet handles
     *                              the request
     *
     * @exception ServletException  if the request for the POST
     *                                  could not be handled
     *
     * @see javax.servlet.ServletOutputStream
     * @see javax.servlet.ServletResponse#setContentType
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_post_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
        } else {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
        }
    }

一般記住這句話就行了:

由伺服器(通過<code>service</code>方法)呼叫,以允許servlet處理POST請求。