Servlet Life Cycle
来源:互联网 发布:db2删除数据库 编辑:程序博客网 时间:2024/05/23 20:59
A servlet follows a certain life cycle. The servlet life cycle is managed by the servlet container. The life cycle contains the following steps:
- Load Servlet Class.
- Create Instance of Servlet.
- Call the servlets
init()
method. - Call the servlets
service()
method. - Call the servlets
destroy()
method.
Step 1, 2 and 3 are executed only once, when the servlet is initially loaded. By default the servlet is not loaded until the first request is received for it. You can force the container to load the servlet when the container starts up though.
Step 4 is executed multiple times - once for every HTTP request to the servlet.
Step 5 is executed when the servlet container unloads the servlet.
Each step is described in more detail below:
Load Servlet Class
Before a servlet can be invoked the servlet container must first load its class definition. This is done just like any other class is loaded.
Create Instance of Servlet
When the servlet class is loaded, the servlet container creates an instance of the servlet.
Typically, only a single isntance of the servlet is created, and concurrent requests to the servlet are executed on the same servlet instance.(单实例多线程) This is really up to the servlet container to decide, though. But typically, there is just one instance.
Call the Servlets init()
Method
When a servlet instance is created, its init()
method is invoked. The init()
method allows a servlet to initialize itself before the first request is processed.
You can specify init
parameters to the servlet in the web.xml
file.
Call the Servlets service()
Method
For every request received to the servlet, the servlets service()
method is called. For HttpServlet
subclasses, one of the doGet()
, doPost()
etc. methods are typically called.
As long as the servlet is active in the servlet container, the service()
method can be called. Thus, this step in the life cycle can be executed multiple times.
Call the Servlets destroy()
Method
When a servlet is unloaded by the servlet container, its destroy()
method is called. This step is only executed once, since a servlet is only unloaded once.
A servlet is unloaded by the container if the container shuts down, or if the container reloads the whole web application at runtime.
- Servlet life cycle
- Servlet Life Cycle
- Servlet的生命周期((life cycle)
- Servlet的生命周期((life cycle)
- Software Development Life-Cycle
- UIComponent之life cycle
- Bug life cycle
- Life Cycle of Object
- Life Cycle of Threads
- Activity life cycle
- Flex Component Life Cycle
- !!!Software release life cycle
- Software release life cycle
- Product Life-cycle Management
- Software release life cycle
- Maven - Build Life Cycle
- Android Activity life Cycle
- Servlets - Life Cycle
- 点阵字幕的显示原理
- 捌月英语
- 使用GenyMotion模拟器+抓包工具SRSniffer分析网络请求
- android无法运行 adb.exe和模拟器不显示hello word
- iOS“断点”(Break Point)你不知道多强大
- Servlet Life Cycle
- POJ 3261 Milk Patterns (后缀数组)
- DRP视频总结
- 打印问题
- 面试题6:重建二叉树
- SDWebImage使用详解
- Java Servlet Example
- DP POJ1160
- JQuery与Ajax的结合进行文本框内容验证