gpt4 book ai didi

java - servlet 覆盖 HttpServlet 服务方法的正当理由

转载 作者:搜寻专家 更新时间:2023-11-01 02:35:58 24 4
gpt4 key购买 nike

我有一些旧的 Servlet 覆盖服务方法,

根据 HttpServlet不应覆盖文档服务方法,仅在特定情况下:

There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).

我找到了几个关于服务方法的答案 calls the do methods

the calls to doGet(), doPost() and the other do-methods are in the HttpServlet class

完成by default并且可以被覆盖

Since your servlet overrides the service() method and provides a different implementation, it doesn't do that anymore. Instead, it does... what the code in the method does.

以及说明原因的答案 not overide it

let servlet handle other methods.

但是我没有任何合理的理由来覆盖这个方法,有没有任何情况?

似乎它可能对应于DRY (Don't repeat yourself)规则,所以所有的do方法都会执行相同的方法。

最佳答案

必须覆盖service如果你需要处理一个非标准的 HTTP 方法,即一个没有被发送到 doXxx 的方法方法 HttpServlet .

作为RFC 2616 HTTP documentation说:

The set of common methods for HTTP/1.1 is defined below. Although this set can be expanded, additional methods cannot be assumed to share the same semantics for separately extended clients and servers.

你自己的引述也说了同样的话,尽管只是隐含地:

There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).

隐含的是,非标准请求必须通过覆盖方法来处理。

关于java - servlet 覆盖 HttpServlet 服务方法的正当理由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com