为什么 Servlet.service() 方法返回 void 而不是 ServletResponse 的实例?
为什么 Servlet 接口中的 service()
方法不返回 ServletResponse 的实例,而是在 ServletResponse 上工作> 容器提供的对象?
简而言之,为什么 Servlet 接口的服务方法是这样的:
public void service(ServletRequest request, ServletResponse response);
而不是这样:
public ServletResponse service(ServletRequest request);
Why does the service()
method in the Servlet interface not return an instance of ServletResponse but rather work on the ServletResponse object provided by the container?
In simple words why is the service method of the Servlet interface like:
public void service(ServletRequest request, ServletResponse response);
and NOT like:
public ServletResponse service(ServletRequest request);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果响应对象是由 servlet 容器提供的,那么它可以控制如何处理诸如缓冲之类的事情。例如,假设您创建了自己的
ServletResponse
- 如果响应超过一定长度,容器将如何管理流式传输响应的能力,而不是缓冲数据?If the response object is provided by the servlet container, it can control how things like buffering are handled. For example, suppose you created your own
ServletResponse
- how would the container manage the ability to stream the response if it's over a certain length, instead of buffering the data?它使用容器为其部分构建的响应。它不会从整体上构建响应。无论如何,这都必须是一场争论。
It uses a Response the container builds partially for it. It doesn't build the response out of whole cloth. It'd have to be an argument in any event.