首页 > 编程知识 正文

servlet什么时候销毁,servlet生命周期三个方法

时间:2023-05-06 07:43:00 阅读:51877 作者:1763

servlet生命周期servlet的生命周期是从servlet出现到销毁的整个过程。 主要分为以下几个阶段。

加载类-实例化-为对象分配空间-初始化-为对象属性分配值-请求处理-服务阶段-放弃

启动服务器时(web.xml中加载启动=1,默认值设置为0 )或首次请求servlet时,将执行初始化方法init(servletconfigconf ) servlet对象将在service(servletrequestREQ,ServletResponse res )方法中运行,最后一次关闭服务器时,servlet对象将被销毁,deservlet对象将被关闭其中无法观察加载阶段,但可以观察初始化、服务和销毁阶段。

初始化publicclassempservletextendshttp servlet {//servlet, 调用init方法@Overridepublic void init () Throwsservletexception ) system.//打开服务@ overrideprotectedvoidservice (httt HttpServletResponse arg1) throws ServletException,io exception } @ overrideprotectedvoiddoget (httpservletrequestreq,HTP seption

思考问题

为什么servlet继承自http servlet,而不是直接实现servlet接口?

解答:

让我们先看看源代码。 http servlet的继承结构

HTP servlet继承了通用servlet。 通用servlet是通用servlet。 他的作用是什么? 通常,实现servlet接口的方法并简化创建servlet的步骤。

通用servlet继承结构:

实现了servlet接口和servlet接口

servlet接口的内容

公共接口servlet { void init (servletconfig var1) throws ServletException; ServletConfig getServletConfig (; 语音服务(servletrequestvar 1,ServletResponse var2) throws ServletException,IOException; 字符串getservletinfo (; void destroy (; }从这里可以看到servlet生命周期的三个主要方法: init、service和destroy。 获取ServletConfig对象的getServletConfig (方法和ServletConfig对象获取servlet的信息的ServletName、ServletContext、init parararation

publicinterfaceservletconfig {字符串get servlet name (; servletcontextgetservletcontext (; stringgetinitparameter (stringvar 1; enumerationstringgetinitparameternames (; }其中,servlet上下文中的对象是servlet上下文中的对象,具有多种功能,可以通过获取servlet上下文中的对象来获取大部分所需信息,如获取servlet的路径。

到目前为止,我已经了解了servlet接口的内容和作用。 总之,可以在三个生命周期中运行的方式获取ServletConfig,在ServletConfig中获取ServletContext。 此外,在通用服务器实现servlet接口后,直接继承通用服务器可以使用上述servlet接口的几种方法。 你可以得到ServletConfig,也可以得到ServletContext,但这很麻烦。 无法直接获取servlet上下文。 因此,除了servlet接口之外,通用servlet还可以直接获取servlet上下文。

> 上图我们观察到,init方法存在两个,一个带参,一个无参

private transient ServletConfig config;public void init(ServletConfig config) throws ServletException {this.config = config;this.init();}public void init() throws ServletException {}public ServletConfig getServletConfig() {return this.config;}

通过这几个方法一起来讲解,首先看init(ServletConfig config)方法,因为只有init(ServletConfig config)中带有ServletConfig对象,为了方便能够在其他地方也能直接使用ServletConfig对象,而不仅仅局限在init(ServletConfig config)方法中,所以创建一个私有的成员变量config,在init(ServletConfig config)方法中就将其赋值给config,然后通过getServletConfig()方法就能够获取ServletConfig对象了,这个可以理解,但是在init(ServletConfig config)中,158行,还调用了一个init()方法,并且这个init()方法是空的,什么读没有,这是为什么呢?这个原因是为了防止一件事情,当我们需要在init方法中做一点别的事情,我们想到的方法就是继承GenericServlet并且重写了init(ServletConfig config)方法,这样依赖,就破坏了原本在GenericServlet类中init(ServletConfig config)写的代码了,也就是在GenericServlet类中的成员变量config会一直是null,无法得到赋值,因为被重写了,就不会在执行GenericServlet中init(ServletConfig config)方法中的代码。要想赋值,就必须在重写的init(ServletConfig config)方法中调用父类的init(ServletConfig config)方法,也就是super.init(ServletConfig config),这样一来,就很不方便,怕有时候会忘了写这句代码,所以在GenericServlet类中增加一个init()方法,以后需要在init方法中需要初始化别的数据,只需要重写init()这个方法,而不需要去覆盖init(ServletConfig config)这个方法,这样设计,就好很多,不用在管init(ServletConfig config)这个其中的内容了。也不用出现其他的问题。

总结:
Servlet的生命周期分为:加载阶段、实例化阶段、初始化阶段、服务阶段、销毁阶段。上面我们说了servlet默认是第一次被访问的时候初始化的,这种情况当用户要使用的时候才创建,也可以在服务器一启动的时候就创建好servlet(这种方式一般不用),要实现这样的操作需要进行配置,在web.xml中进行配置

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。