doPut()详解

先看它与post的区别:

PUTPOST 方法的区别在于,PUT方法是幂等的:调用一次与连续调用多次是等价的(即没有副作用),而连续调用多次POST方法可能会有副作用,比如将一个订单重复提交多次。

注释的中文意思为:

服务器调用(通过<code>service</code>方法)以允许servlet处理PUT请求。

PUT操作允许客户端在服务器上放置文件,类似于通过FTP发送文件。重写此方法时,保留随请求发送的任何内容头部(包括内容长度、内容类型、内容传输编码、内容编码、内容基础、内容语言、内容位置、内容MD5和内容范围)。如果方法不能处理内容头,则必须发出错误消息(HTTP 501-Not Implemented)并丢弃请求。

有关HTTP 1.1的更多信息,请参阅RFC 2616<href="http://www.ietf.org/rfc/rfc2616.txt"></a>.<p>此方法不需要是安全的或幂等的。

执行<code>doPut</code>的操作可能具有副作用,用户可以对此负责。当使用此方法时,它会将受影响的URL的副本保存在临时存储中。

    /**
     * Called by the server (via the <code>service</code> method)
     * to allow a servlet to handle a PUT request.
     *
     * The PUT operation allows a client to
     * place a file on the server and is similar to
     * sending a file by FTP.
     *
     * <p>When overriding this method, leave intact
     * any content headers sent with the request (including
     * Content-Length, Content-Type, Content-Transfer-Encoding,
     * Content-Encoding, Content-Base, Content-Language, Content-Location,
     * Content-MD5, and Content-Range). If your method cannot
     * handle a content header, it must issue an error message
     * (HTTP 501 - Not Implemented) and discard the request.
     * For more information on HTTP 1.1, see RFC 2616
     * <a href="http://www.ietf.org/rfc/rfc2616.txt"></a>.
     *
     * <p>This method does not need to be either safe or idempotent.
     * Operations that <code>doPut</code> performs can have side
     * effects for which the user can be held accountable. When using
     * this method, it may be useful to save a copy of the
     * affected URL in temporary storage.
     *
     * <p>If the HTTP PUT request is incorrectly formatted,
     * <code>doPut</code> returns an HTTP "Bad Request" message.
     *
     * @param req   the {@link HttpServletRequest} object that
     *                  contains the request the client made of
     *                  the servlet
     *
     * @param resp  the {@link HttpServletResponse} object that
     *                  contains the response the servlet returns
     *                  to the client
     *
     * @exception IOException   if an input or output error occurs
     *                              while the servlet is handling the
     *                              PUT request
     *
     * @exception ServletException  if the request for the PUT
     *                                  cannot be handled
     */
    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

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

补充相关put与post区别的相关博客:https://www.cnblogs.com/kungfupanda/p/5525675.html


版权声明:本文为qq_29519041原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。