HttpServlet的生命周期是什么? [英] What is the lifecycle of a HttpServlet?

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

问题描述

基本上,servlet的实例有多长?我有点猜测它是会话范围。但是,我想它可能有某种超时或垃圾收集来删除旧实例。

Basically, how long is an instance of a servlet around for? I am kind of guessing it is session scope. However, I suppose it could have some sort of timeout or garbage collection to remove old instances.

推荐答案


  • 在应用程序启动时(它部署在servlet容器上)或首次访问时创建一个servlet(取决于 load-on-startup 设置)

  • 当servlet被实例化时,servlet的 init()方法被称为

  • 然后servlet(它的唯一实例)处理所有请求(由多个线程调用的 service()方法)。这就是为什么不建议在其中进行任何同步,并且当取消部署应用程序(servlet容器停止)时,你应该避免使用servlet的实例变量

  • destroy()方法被调用。

    • a servlet is created when the application starts (it is deployed on the servlet container) or when it is first accessed (depending on the load-on-startup setting)
    • when the servlet is instantiated, the init() method of the servlet is called
    • then the servlet (its one and only instance) handles all requests (its service() method being called by multiple threads). That's why it is not advisable to have any synchronization in it, and you should avoid instance variables of the servlet
    • when the application is undeployed (the servlet container stops), the destroy() method is called.
    • 这篇关于HttpServlet的生命周期是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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