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

查看:29
本文介绍了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 容器上)或第一次访问时(取决于 load-on-startup 设置)创建一个 servlet
  • 当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天全站免登陆