Service 和 doGet 和 doPost 方法的区别

Service 和 doGet 和 doPost 方法的区别

  • Service方法

不管是 get 方式还是 post 方式的请求,如果 Servlet 类中有service 方法,则优先调用 Service 方法。

  • doGet 方法
protected void doGet(HttpServletRequest req, 
                   HttpServletResponse resp)
        throws ServletException, IOException

在没有 service 方法的情况下如果是 get 方式的请求所调用的处理请求的方法

  • doPost 方法

在没有 service 方法的情况下如果是 post 方式的请求所调用的处理请求的方法


官方API上的解释

https://docs.oracle.com/javaee/7/api/toc.htm
浏览器内查找HttpServlet类即可快速定位

  • service
protected void doDelete(HttpServletRequest req,
              HttpServletResponse resp)
        throws ServletException, IOException 

Dispatches client requests to the protected service method. There's no need to override this method.
将客户端请求分派给受保护的服务方法。没有必要覆盖此方法。

  • doGet

Called by the server (via the service method) to allow a servlet to handle a GET request.
由服务器调用(通过服务方法)以允许servlet处理GET请求。
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.
重写此方法以支持GET请求也会自动支持HTTP HEAD请求。HEAD请求是一个GET请求,它在响应中不返回任何主体,只返回请求头字段。
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 PrintWriter object to return the response, set the content type before accessing the PrintWriter object.
覆盖此方法时,请读取请求数据,编写响应头,获取响应的编写器或输出流对象,最后编写响应数据。最好包含内容类型和编码。使用PrintWriter对象返回响应时,请在访问PrintWriter对象之前设置内容类型。
The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.
servlet容器必须在提交响应之前写入标头,因为在HTTP中,标头必须在响应主体之前发送。
Where possible, set the Content-Length header (with theServletResponse.setContentLength(int) 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.
在可能的情况下,设置Content-Length标头(使用ServletResponse.setContentLength(int)方法),以允许servlet容器使用持久连接将其响应返回给客户端,从而提高性能。如果整个响应适合响应缓冲区,则自动设置内容长度。
When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header.
使用HTTP 1.1分块编码(这意味着响应具有Transfer-Encoding标头)时,请不要设置Content-Length标头。
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.
GET方法应该是安全的,即没有任何副作用,用户对此负责。例如,大多数表单查询没有副作用。如果客户端请求旨在更改存储的数据,则该请求应使用其他一些HTTP方法。
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.
GET方法也应该是幂等的,这意味着它可以安全地重复。有时使方法安全也使其成为幂等的。例如,重复查询既安全又幂等,但在线购买产品或修改数据既不安全也不是幂等。
If the request is incorrectly formatted, doGet returns an HTTP "Bad Request" message.
如果请求格式不正确,doGet将返回HTTP“错误请求”消息。

  • doPost
 protected void doPost(HttpServletRequest req, 
                HttpServletResponse resp)
        throws ServletException, IOException

Called by the server (via the service 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.
由服务器调用(通过服务方法)以允许servlet处理POST请求。HTTP POST方法允许客户端一次性向Web服务器发送无限长度的数据,并且在发布信用卡号等信息时非常有用。
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 PrintWriter object to return the response, set the content type before accessing the PrintWriter object.
覆盖此方法时,请读取请求数据,编写响应头,获取响应的编写器或输出流对象,最后编写响应数据。最好包含内容类型和编码。使用PrintWriter对象返回响应时,请在访问PrintWriter对象之前设置内容类型。
The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.
servlet容器必须在提交响应之前写入标头,因为在HTTP中,标头必须在响应主体之前发送。
Where possible, set the Content-Length header (with theServletResponse.setContentLength(int) 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.
在可能的情况下,设置Content-Length标头(使用ServletResponse.setContentLength(int)方法),以允许servlet容器使用持久连接将其响应返回给客户端,从而提高性能。如果整个响应适合响应缓冲区,则自动设置内容长度。
When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header.
使用HTTP 1.1分块编码(这意味着响应具有Transfer-Encoding标头)时,请不要设置Content-Length标头。
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.
此方法不需要是安全的或幂等的。通过POST请求的操作可能具有可以使用户负责的副作用,例如,更新存储的数据或在线购买物品。
If the HTTP POST request is incorrectly formatted, doPost returns an HTTP "Bad Request" message.
如果HTTP POST请求格式不正确,doPost将返回HTTP“错误请求”消息。

备注:翻译来字谷歌翻译

你可能感兴趣的:(Service 和 doGet 和 doPost 方法的区别)