- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一些旧的 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 (thedoXXX
methods listed above).
隐含的是,非标准请求必须通过覆盖方法来处理。
关于java - servlet 覆盖 HttpServlet 服务方法的正当理由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492343/
我被告知这不是一个好的做法 @Autowired HttpServlet httpServlet; 在 Spring Boot 中,因为它只会为所有点击创建一个对象。我对 Spring Boot 和
我想知道反序列化后是否调用了 HttpServlet 的 init 方法。 我不知道是否有任何容器曾经序列化一个 servlet,但它实现了 Serializable 接口(interface),所以
我在 HttpServlet 文档中读到 HttpServlet 的子类必须重写至少一个方法,通常是以下方法之一: doGet, if the servlet supports HTTP GET re
我有几个在服务器端执行操作的 servlet。在一些上,我只是编码一些不必要的数据并将其发送回来,这似乎毫无意义。你必须回应吗?当您只说 return 时会发生什么?我以前这样做过,似乎没有什么问题,
我尝试创建一个文件,写入文件,然后将文件转换为输入流,并将其字节传输到 HTTP 响应的输出流。但我收到消息“/tmp/mozilla_xxxx/33JJ1OHw.md.part 无法保存,因为无法读
我正在开发一个具有两个 Web 界面的 Java 应用程序:一个处理某些 Web 服务逻辑的 servlet (/WSProxy) 和一个使用 Spring 框架的 Web 管理界面(/Admin 或
我在 Java EE 应用程序中使用 Apache Tomcat v8.0 服务器。基本上,我创建了一个 ResponseUpload servlet。我需要通过 POST 请求从 Web 应用程序获
这个问题在这里已经有了答案: How many instances are created for a HTTP servlet (2 个答案) 关闭 6 年前。 根据servlet的生命周期,se
我有一个 Web 应用程序,其中多个 servlet 使用一定数量的相同逻辑进行预初始化(设置日志记录、 session 跟踪等)。我所做的是在 javax.servlet.http.HttpServ
我有一个应用程序,用户正在访问 servlet 文件,该文件将有条件地读取 .pdf 文件并发送 .pdf 文件作为对用户请求的响应。 servlet 代码不会设置响应 header 的内容长度。 在
这个问题在这里已经有了答案: Http Servlet request lose params from POST body after read it once (13 个答案) 关闭 7 年前。
基本上,一个 servlet 实例存在多长时间?我有点猜测它是 session 范围。但是,我想它可能有某种超时或垃圾收集来删除旧实例。 最佳答案 当应用程序启动时(部署在 servlet 容器上)或
我需要一些帮助,我的意思是对这个继承设计的意见。 我创建了Controller.class,它扩展了HttpServlet和AjaxController.class 扩展了 Controller.cl
我想在扩展 HttpServlet 的类上注入(inject) DAO 依赖项,这可能吗?我不想从应用程序上下文中手动获取依赖项,但如果可能的话,希望在我的 Servlet 实例上进行真正的依赖项注入
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
假设,我有一个 TestServlet 类,它声明为全局字符串变量。如果同时调用 TestServlet 类,则此“消息”字符串变量是否可以安全地打印而无需从并发线程交叉? public class
我有一个扩展 HttpServlet 类的类。我知道 Java 是多线程的,不同的线程(阅读:HTTP 请求)可以使用同一个实例。我想在第一个请求时实例化一个对象并使用它直到 session 结束。
出于记录目的,我重写了 HttpServlet 的服务方法,如下所示: @Override protected void service(HttpServletRequest req, HttpSer
我对在 Google App Engine 上遇到的这个问题感到困惑。 我创建了几个servlet,因此我创建了一个基类来完成一些基本的重复工作。像这样: public class MyBase ex
我想用 http://www.easymock.org/ 测试我的 servlet 如何编写单元测试代码? 我根据您的回复更新我的代码。 我刚刚使用 Google 并制作了这段代码。 这是我的 Ser
我是一名优秀的程序员,十分优秀!