servlet 覆盖 HttpServlet 服务方法的正当理由
Valid reason for servlet to override HttpServlet service method
我有一些旧的 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).
我找到了几个关于服务方法的答案
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方法都会执行相同的方法。
如果你需要处理一个非标准的 HTTP 方法,你 必须 覆盖 service
11=] 方法 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).
隐含的是,非标准请求必须通过覆盖方法来处理。
我有一些旧的 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).
我找到了几个关于服务方法的答案
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方法都会执行相同的方法。
如果你需要处理一个非标准的 HTTP 方法,你 必须 覆盖 service
11=] 方法 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 (thedoXXX
methods listed above).
隐含的是,非标准请求必须通过覆盖方法来处理。