此方法中注释的意思为:
由服务器(通过<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请求。