资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Servlet生命周期是什么

小编给大家分享一下Servlet生命周期是什么,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

成都创新互联网站建设提供从项目策划、软件开发,软件安全维护、网站优化(SEO)、网站分析、效果评估等整套的建站服务,主营业务为成都网站制作、网站设计,app软件开发以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。成都创新互联深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

Servlet的生命周期是由它部署的容器控制的。当一个请求映射到一个Servlet,Servlet容器执行下面的步骤。  
1  如果不存在这个Servlet的实例,容器执行:
 a. 加载Servlet类
 b. 创建Servlet类实例
 c. 通过调用init方法初始化Servlet(initialization is covered in Creating and Initializing a Servlet)
2  容器调用Servlet的service方法,传入requset和response参数。Service方法会在 Writing Service Methods章节探讨。  
如果它需要移除这个Servlet,则容器通过调用Servlet的destroy方法来完成。更多信息需要查看Finalizing a Servlet章节。

管理Servlet生命周期事件

你可以通过定义生命周期事件发生方法的监听器来监听Servlet的生命周期并做出反应。使用这些监听对象,你必须定义并且具体描述这些监听类。

定义监听类

你定义的监听类是必须是监听接口的一个实现。下面的列表定义了可以监听的事件和对应的必须实现的规范的接口。当监听方法被调用,它将传入一个适合该事件的容器的信息的事件。例如,一个HttpSessionListener接口的方法会被传入一个HttpSessionEvent,HttpSessionEvent包含了一个HttpSession。  
Servlet 生命周期事件列表

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction  
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

使用@WebListener注解可以获取特定web程序的各种事件。使用@WebListener注解的类必须实现下列的接口之一。

javax.servlet.ServletContextListener
javax.servlet.ServletContextAttributeListener
javax.servlet.ServletRequestListener
javax.servlet.ServletRequestAttributeListener
javax.servlet..http.HttpSessionListener
javax.servlet..http.HttpSessionAttributeListener

处理Servlet异常

Servlet执行时,可能发生任意数量的异常。当一个异常发生时,web容器自动生成包含下述信息的默认页面:

A Servlet Exception Has Occurred

但你也可以定义特定异常的特定页面让容器返回。

以上是Servlet生命周期是什么的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


文章标题:Servlet生命周期是什么
标题链接:http://cdkjz.cn/article/gsidji.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220