doGet()详解

doGet方法源码

    /**
     * Called by the server (via the <code>service</code> method) to
     * allow a servlet to handle a GET request.
     *
     * <p>Overriding this method to support a GET request also
     * automatically supports an HTTP HEAD request. A HEAD
     * request is a GET request that returns no body in the
     * response, only the request header fields.
     *
     * <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>The GET method should be safe, that is, without
     * any side effects for which users are held responsible.
     * For example, most form queries have no side effects.
     * If a client request is intended to change stored data,
     * the request should use some other HTTP method.
     *
     * <p>The GET method should also be idempotent, meaning
     * that it can be safely repeated. Sometimes making a
     * method safe also makes it idempotent. For example,
     * repeating queries is both safe and idempotent, but
     * buying a product online or modifying data is neither
     * safe nor idempotent.
     *
     * <p>If the request is incorrectly formatted, <code>doGet</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 GET request
     *
     * @exception ServletException  if the request for the GET
     *                                  could not be handled
     *
     * @see javax.servlet.ServletResponse#setContentType
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_get_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处理GET请求。

重写此方法以支持GET请求还自动支持HTTP HEAD请求。HEAD请求是一个GET请求,它在响应中不返回主体,只返回请求头字段。

重写此方法时,读取请求数据,写入响应头,获取响应的写入器或输出流对象,最后写入响应数据。最好包括内容类型和编码。当使用<code>PrintWriter</code>对象返回响应时,在访问<code>PrintWriter</code>对象之前设置内容类型。

servlet容器在提交响应之前必须写头部,因为在HTTP中,头必须在响应主体之前发送。

如果可能,设置Content-Length头(使用@link javax.servlet.servletResponse setContentLength方法),以允许servlet容器使用持久连接将其响应返回到客户端,从而提高性能。如果整个响应适合响应缓冲区,则自动设置内容长度。

当使用HTTP 1.1分块编码(这意味着响应具有Transfer-Encoding报头)时,不要设置Content-Length报头。

GET方法应该是安全的,也就是说,没有任何用户会承担它的副作用。例如,大多数表单查询不会有副作用。如果客户机请求要更改存储的数据,则该请求应该使用其他一些HTTP方法。

(博主补充,说的就是get方法刷新页面不会导致表单重复提交,与post方法所对应)

get方法也应该是等幂的,这意味着它可以安全地重复。有时,使方法安全也会使其为等幂。例如,重复查询既安全又具有等量性,但是在线购买产品或修改数据既不安全也不等量。

博主补充:等幂:在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。

.

一般情况,我们只需要知道它用于处理 客户端发来的get类型的请求,以及他与post的区别就够了。

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值