`

tomcat工作原理(第二章)A Simple Servlet Container

 
阅读更多
要知道Servlet容器怎么工作,需要熟悉Servlet接口和(待完善)。
所有的Servlet都必须实现Servlet接口或者继承(待完善)。
Servlet接口有五个方法,分别如下:
public void init(ServletConfig config) throws ServletException;
public void service(ServletRequest request,ServletResponse response) throws ServletException,IOException;
public void destroy();
public ServletConfig getServletConfig();
public java.lang.String getServletInfo();
在Servlet的五个方法中,init,service 和destroy方法是控制servlet生命周期的方法。
init方法,是在Servlet被Servlet容器实例化之后调用的,这个方法被调用一次预示着这个servlet要去提供服务。
在Servlet接收任何请求之前,它的初始化方法必须被成功的完成。一个Servlet程序员会覆盖这个方法,写一些初始化代码,初始化代码只需要执行一次,比如加载数据库驱动、初始化一些数值等等。在其他的一些情况下,这个方法通常都是空的。
任何时候,当有请求的时候,servlet容器会调用servlet的service方法,servlet容器转换ServletRequest对象和ServletResponse对象。ServletRequest对象包含客户端HTTP请求的信息,ServletResponse对象封装了服务器端的响应信息。在servlet的生命周期中,service方法会被调用多次。
servlet容器将一个servlet从服务中移除之前会调用该servlet的destroy方法。
这种情况通常发生在servlet容器关闭或者servlet 容器需要释放内存的时候。
这个方法只有在位于servlet的service方法中是所有线程都退出(exit),或者是已经过了一个超时的时间段。在Servlet容器调用某个Servlet的destroy方法之后,它就不会再调用该Servlet的service方法。destroy方法给了servlet容器一个释放它所持有的资源的机会,比如内容、文件、handles,还有线程,而且保证所有的持久化状态都和Servlet当前在内存中的状态是一致的。
分享到:
评论
1 楼 love_miaohong 2012-11-05  
    

相关推荐

    How Tomcat Works: A Guide to Developing Your Own Java Servlet Container

    第2章 一个简单的servlet容器 7 2.1 简述 7 2.2 javax.servlet.Servlet接口 7 2.3 Application 1 7 2.3.1 HttpServer1类 8 2.3.2 Request类 8 2.3.3 Response类 9 2.3.4 StaticResourceProcessor类 9 2.3.5 ...

    《深入剖析Tomcat(中文版+英文版)》.rar

    《深入剖析Tomcat》深入剖析Tomcat 4和Tomcat 5中的每个组件(如果TOMCAT版本有点老,不过现在的Tomcat6和7同样可以借鉴参考),并揭示其内部工作原理。通过学习《深入剖析Tomcat》,你将可以自行开发Tomcat组件,或者...

    Tomcat The Definitive Guide, 2nd Edition(PDF)

    It takes a book as versatile as its subject to cover Apache Tomcat, the popular open source Servlet and JSP container and high performance web server. Tomcat: The Definitive Guide is a valuable ...

    how-tomcat-works

    第2章 一个简单的servlet容器 7 2.1 简述 7 2.2 javax.servlet.Servlet接口 7 2.3 Application 1 7 2.3.1 HttpServer1类 8 2.3.2 Request类 8 2.3.3 Response类 9 2.3.4 StaticResourceProcessor类 9 2.3.5 ...

    Tomcat 5 Unleashed

    Tomcat is a fine piece of software, and does its job as a servlet container extremely well. What I want to show in this book is how to use Tomcat in a production, enterprise environment. You'll see ...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Building a Simple Web Application Section 1.7. Sharing Data Among Web Applications Chapter 2. Controlling Web Application Behavior with web.xml Section 2.1. Purpose of the Deployment Descriptor ...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Building a Simple Web Application Section 1.7. Sharing Data Among Web Applications Chapter 2. Controlling Web Application Behavior with web.xml Section 2.1. Purpose of the Deployment Descriptor ...

    spring-boot-reference.pdf

    27.4. Embedded Servlet Container Support 27.4.1. Servlets, Filters, and listeners Registering Servlets, Filters, and Listeners as Spring Beans 27.4.2. Servlet Context Initialization Scanning for ...

    java 面试题 总结

    Stateless Session Bean 虽然也是逻辑组件,但是他却不负责记录使用者状态,也就是说当使用者呼叫 Stateless Session Bean 的时候,EJB Container 并不会找寻特定的 Stateless Session Bean 的实体来执行这个 method...

    271个java需要用的jar包

    plexus-container-default-1.0-alpha-10.jar plexus-utils-1.2.jar poi-3.12-20150511.jar poi-ooxml-3.12-20150511.jar poi-ooxml-schemas-3.12-20150511.jar quartz-all-1.8.5.jar saaj.jar serializer-2.7.0.jar ...

    超级有影响力霸气的Java面试题大全文档

    抽象包括两个方面,一是过程抽象,二是数据抽象。 2.继承:  继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承...

    dubbo、dubbox编译所需jar包

    cxf-rt-frontend-simple-2.6.1.jar cxf-rt-transports-http-2.6.1.jar doxia-core-1.0-alpha-11.jar doxia-core-1.0.jar doxia-decoration-model-1.0-alpha-11.jar doxia-decoration-model-1.0.jar doxia-module-apt...

Global site tag (gtag.js) - Google Analytics