`

Servlet 的生命周期图

    博客分类:
  • JSP
阅读更多
A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet

    The servlet is initialized by calling the init () method.
    The servlet calls service() method to process a client's request.
    The servlet is terminated by calling the destroy() method.
    Finally, servlet is garbage collected by the garbage collector of the JVM.

Now let us discuss the life cycle methods in details.

The init() method :
The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.

The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started.

When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply creates or loads some data that will be used throughout the life of the servlet.

The init method definition looks like this:

public void init() throws ServletException {
  // Initialization code...
}




The service() method :
The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.

Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

Here is the signature of this method:

public void service(ServletRequest request, 
                    ServletResponse response) 
      throws ServletException, IOException{
}


The service () method is called by the container and service method invokes doGe, doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.




The doGet() and doPost() are most frequently used methods with in each service request. Here is the signature of these two methods.


The doGet() Method
A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method.

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet code
}


The doPost() Method
A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.

public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet code
}



The destroy() method :
The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.

After the destroy() method is called, the servlet object is marked for garbage collection. The destroy method definition looks like this:

  public void destroy() {
    // Finalization code...
  }





Architecture Digram:
The following figure depicts a typical servlet life-cycle scenario.

    First the HTTP requests coming to the server are delegated to the servlet container.
    The servlet container loads the servlet before invoking the service() method.
    Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.



  • 大小: 20.8 KB
分享到:
评论

相关推荐

    Servlet-Servlet生命周期

    ### Servlet生命周期详解 #### 一、引言 在Java Web开发中,Servlet作为一种重要的技术,被广泛应用于构建动态网页和处理客户端请求。了解Servlet的生命周期对于深入理解和掌握Servlet的工作机制至关重要。本文将...

    servlet生命周期详解

    初始化阶段是整个Servlet生命周期的第一个关键步骤,它标志着Servlet的开始。此阶段主要由以下步骤组成: 1. **Servlet容器加载Servlet类**:Servlet容器负责加载Servlet类,并将.Class文件的数据读入内存。这一...

    SERVLET生命周期与JSP生命周期比较

    ### SERVLET生命周期与JSP生命周期比较 #### 一、引言 在现代Web开发中,Servlet和JSP是两种非常重要的技术,它们都属于Java EE平台的一部分,主要用于构建动态Web应用程序。这两种技术各有特点,但又紧密相关,...

    servlet生命周期演示代码

    ### Servlet 生命周期演示代码详解 #### 一、Servlet 生命周期概述 在深入分析代码之前,我们先来了解一下 Servlet 的生命周期。Servlet 的生命周期主要包括三个阶段:初始化 (`init` 方法)、请求处理 (`service` ...

    Servlet 介绍 以及Servlet生命周期(详细)

    **Servlet生命周期** Servlet的生命周期可以分为三个主要阶段:初始化、服务和销毁。 1. **初始化阶段**: - 当Servlet首次被请求或者在web应用启动时,容器(如Tomcat)会加载Servlet类,并调用`init()`方法进行...

    201340206_蔡晓双_servlet生命周期图.vsdx

    201340206_蔡晓双_servlet生命周期图.vsdx

    解读servlet生命周期

    Servlet生命周期是Java Web开发中一个关键的概念,它描述了Servlet从创建到销毁的整个过程,这个过程由Servlet容器(如Tomcat)进行管理。Servlet生命周期主要分为三个阶段:初始化阶段、运行阶段和销毁阶段。 1. ...

    Servlet生命周期示意图

    关于对Servlet声明周期的图例示意!

    servlet生命周期详细图解(矢量图)

    servlet生命周期详细图解,矢量图。 详细的解释请参考本人博客:http://blog.csdn.net/dwyers/article/details/38435949

    Servlet生命周期

    ### Servlet 生命周期详解 #### 一、概述 Servlet 是 Java Web 开发的核心技术之一,用于处理客户端的 HTTP 请求并返回响应。了解 Servlet 的生命周期对于更好地控制和优化 Web 应用程序至关重要。Servlet 的生命...

    servlet生命周期和模版设计模式

    Servlet生命周期和模板设计模式是Java Web开发中的两个关键概念,它们在构建动态Web应用程序时起着至关重要的作用。 首先,让我们深入理解Servlet的生命周期。Servlet是Java编程语言中的一种接口,用于扩展服务器的...

    Servlet 生命周期.pdf

    在Servlet生命周期中,init()方法只会被调用一次,service()方法会在每次用户请求时被调用,destroy()方法只会被调用一次,在Servlet生命周期结束时被调用。Servlet生命周期的正确实现是保证Servlet正确运行的关键。

    servlet生命周期

    #### 三、Servlet生命周期详解 ##### 实例化 - **按需创建**:默认情况下,当第一次HTTP请求到达时,容器会创建Servlet的实例。 - **预加载**:通过在`web.xml`中配置`<load-on-startup>`标签,可以在应用启动时...

    Java WEB 篇七 Servlet 生命周期.xmind

    Java WEB 篇七 Servlet 生命周期

    Servlet笔记,servlet生命周期、定义

    Servlet的生命周期指的是从创建到销毁的过程。这个过程包括以下几个阶段: 1. 初始化阶段:通过调用init()方法来完成,这个方法在Servlet创建后被调用一次。 2. 处理请求阶段:通过调用service()方法处理客户端请求...

    说一说servlet的生命周期

    这个方法也是Servlet生命周期中的最后一次调用,之后Servlet实例会被垃圾收集器回收。 在实际开发中,理解Servlet的生命周期对于优化性能和管理资源至关重要。例如,可以通过控制Servlet的实例化次数来减少内存消耗...

Global site tag (gtag.js) - Google Analytics