网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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://bjjierui.cn/article/gsidji.html

其他资讯