HttpServletRequest 对象的生命周期是什么? [英] What is a life of HttpServletRequest object?

查看:99
本文介绍了HttpServletRequest 对象的生命周期是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 HttpServletRequest 生命对象有疑问.request 对象进入controller 后是否销毁?

I have doubt about HttpServletRequest life object. Is the request object destroyed after it got into controller?

推荐答案

HttpServletRequest 对象的生命周期就是:服务 HTTP Servlet 请求的时间.

The lifetime of an HttpServletRequest object is just that: the time of serving an HTTP Servlet request.

它可以在调用 servlet 的 doGet()doPost() 等方法之前创建,并且可以在调用之后立即销毁.仅在服务请求期间使用它才有效.

It may be created right before calling the servlet's doGet(), doPost() etc. methods, and may be destroyed right after that. It is only valid to use it during serving a request.

注意:然而,Servlet 容器可能为多个请求重用 HttpServletRequest 对象(这是典型的情况),但它们将是清除"或重置,以便在请求之间不会泄漏对象(如参数或属性).这仅仅是由于性能问题:重置 HttpServletRequest 对象比丢弃现有对象并创建新对象要快得多且成本更低.

Note: However Servlet containers may reuse HttpServletRequest objects for multiple requests (and this is typically the case), but they will be "cleaned" or reset so no objects (like parameters or attributes) will leak between requests. This is simply due to performance issue: it is much faster and cheaper to reset an HttpServletRequest object than to throw away an existing one and create a new one.

在典型的 Servlet 容器实现中,如果一个 HTTP 请求进来,当 Servlet 容器解析请求的 HTTP 输入数据时,就会创建一个 HttpServletRequest.整个请求可能会被延迟初始化(例如,如果参数确实被访问,例如通过 getParameter() 方法,则可能只对其进行解析和填充).然后这个 HttpServletRequest(它扩展了 ServletRequest)通过 Servlet 过滤器,然后传递到 Servlet.service() 它将调度调用doGet(), doPost() 等基于 HTTP 方法 (GET, POST, PUT 等).然后请求将仍然有效,直到请求-响应对在整个过滤器链中循环返回.然后它会被销毁或重置(在它被用于另一个 HTTP 请求之前).

In a typical Servlet container implementation if an HTTP request comes in, an HttpServletRequest is created right when the HTTP input data of the request is parsed by the Servlet container. The whole request might be lazily initialized (e.g. parameters might only be parsed and populated if they are really accessed e.g. via getParameter() method). Then this HttpServletRequest (which extends ServletRequest) is passed through the Servlet filters and then passed on to Servlet.service() which will dispatch the call to doGet(), doPost() etc based on the HTTP method (GET, POST, PUT etc.). Then the request will still be alive until the request-response pair cycles back throughout the filter chain. And then it will be destroyed or reset (before it is used for another HTTP request).

这篇关于HttpServletRequest 对象的生命周期是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆