此方法中注釋的意思為:
由伺服器(通過<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請求。