《Head First Servlets & JSP(中文版)》章节试读

当前位置:首页 > 网络编程 > 编程语言与程序设计 > Head First Servlets & JSP(中文版)章节试读

出版社:中国电力出版社
出版日期:2006-10
ISBN:9787508342849
作者:(美)巴萨姆,(美)塞若,(美)贝茨
页数:854页

《Head First Servlets & JSP(中文版)》的笔记-第97页 - 第四章-这一章是讲servlet的

servlet类的生命周期,包含以下几个方面的内容:
1、什么时候加载类。
2、什么时候执行构造器。
答:如果没有配置启动http服务器的时候就加载servlet类的话,那么加载和执行构造器都是在第一次请求该servlet时;
如果配置了,那么就是启动http服务器时,就加载和执行构造器。
3、什么时候初始化数据,什么时候销毁数据?
答:
1)什么时候初始化数据,同上。
2)什么时候销毁数据,这里的销毁数据分为两种,一种是servlet对象本身,一种是其他的数据:
a. 其他的数据就不用多说了,和初始化数据类似、差不多。
b. 销毁servlet对象本身,这个有点说头,首先servlet类在http服务器中是单例,前面已经说了什么时候执行构造器(执行构造器,就是new 了一个对象啊),现在要销毁这个对象,为什么要销毁这个对象呢,我们程序员一般是不会去销毁servlet对象的,如果真的有这种需求的话(按理来说,确实有这种需求,就是我们不再访问这个servlet时就可以把它销毁掉),那么程序员如何去销毁它呢,这个暂时我也不知道(据目前所知,servlet生命周期由http服务器全权负责,也就是说,只有http服务器才能调用destroy方法去销毁servlet对象),以后再研究一下。
那么,http服务器什么时候才会销毁servet对象呢,就是关闭http服务器的时候会调用(而且仅调用一次,这是废话)destory方法,http服务器调用destroy方法有2个目的,第一,是销毁servlet对象之前清除各种数据;第二,执行完destroy方法之后,http服务器才会将servlet对象销毁掉。
附:void destroy()方法的API说明
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.
This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

4、servlet对象能活多久?
终上所述,一般来说,它一直是活着的(它一直在等待浏览器发出请求),直到http服务器关闭。

《Head First Servlets & JSP(中文版)》的笔记-第182页 - 属性和监听者

了解了ServletContextListener, 用来初始化Web应用的初始参数,是spring、struts等Web框架初始化的入口,在web.xml中用listener来配置。

《Head First Servlets & JSP(中文版)》的笔记-第95页 - 第四章

http服务器:等待浏览器发出请求。
1、创建请求对象。
2、创建响应对象。
总结:http服务器的任务就是,负责创建请求对象和响应对象,并且将请求对象和响应对象传递servlet类的service()方法作为参数。

http服务器,还有一个任务是负责为每个/次请求创建一个新的线程。


 Head First Servlets & JSP(中文版)下载 更多精彩书评


 

外国儿童文学,篆刻,百科,生物科学,科普,初中通用,育儿亲子,美容护肤PDF图书下载,。 零度图书网 

零度图书网 @ 2024