资讯专栏INFORMATION COLUMN

Servlet生命周期

littlelightss / 3333人阅读

摘要:的生命周期是由它部署的容器控制的。管理生命周期事件你可以通过定义生命周期事件发生方法的监听器来监听的生命周期并做出反应。生命周期事件列表使用注解可以获取特定程序的各种事件。处理异常执行时,可能发生任意数量的异常。

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

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73864.html

相关文章

  • Servlet教程------【Cheryl的Java技术栈系列】

    摘要:实际开发中最常见的和方法配置文件配置文件如下引用百度定义 Servlet全解析笔记 Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容,Servlet的常见类型有,继承自Httpservlet的普通Servlet类、Se...

    jimhs 评论0 收藏0
  • JavaWEB开发07——Http协议&Servlet

    摘要:协议版本请求头客户端向服务器端表示,我能支持什么类型的数据。在地址栏上输入项目名称向报告,我这个应用里面有这个,名字叫做具体的路径是注册的映射。的配置,通过这个对象,可以获取在配置的时候一些信息先说,在写怎么用,最后说有什么用。 1.Http协议 什么是协议 双方在交互、通讯的时候, 遵守的一种规范、规则。 http协议 针对网络上的客户端 与 服务器端在执行http请求的时候...

    hsluoyz 评论0 收藏0
  • 基础知识: Java servlet

    摘要:使用技术开发应用程序深入了解的机制对应用的开发将有重要的推动作用而想深入了解的机制就不得不了解包包中包含了个接口个类和个异常类它们分别是接口和类和异常类和的生命周期在的接口中定义了一个的生命周期方法分别是和演示了生命周期方法的简单在中如何获 使用 Java 技术开发 WEB 应用程序 , 深入了解 Servlet 的机制对应用的开发将有重要的推动作用 . 而想深入了解 Servlet ...

    biaoxiaoduan 评论0 收藏0

发表评论

0条评论

littlelightss

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<