`

Servlet之 destroy

    博客分类:
  • JSP
阅读更多
只听说过 Servlet 何时 init(),何时 service(),却没有仔细想过何时被 destroy()

____________________________________________________________________


When exactly a servlet is destroyed?

I'm checking a Java Servlet tutorial, but I miss the information of when exactly the servlet is destroyed by the server? and what if I want to destroy manually an unused Servlet to conserve a memory for other tasks!

Because as I know every server has its limit in memory and hosting unused servlets is wasting of resources and application quality,

Thank you to clarify this point because the application performance is one of the most important issue to care about during the development process!


-------
Answer:
-------

Read about the Servlet Lifecycle; don't destroy it manually. Also, the container will do it if you deploy a new version of the application. Generally one Servlet instance handles many concurrent requests.

-------------------------------------

If you could destroy your servlet, the next client would have to wait for it to deploy and init. The clients share one servlet instance. Of course your application performance depends on the servlet container. Your application runs inside the container.

-------------------------------------

There is only one instance of the Servlet on each node in multi-clustered environment or you can say there is only one instance of each Servlet on each JVM machine.

Servlet is initialized on application startup or the fist time the servlet is invoked.

All the Servlet instances are destroyed when server is shutting down or on application disposal.

You can't destroy the Servlet manually and Servlet is just like worker not for data container. In most of the cases Servlet doesn't contain any instance members to avoid multi-threading issues.


-------------------------------------

The Servlet specification does not say when a servlet must be shut down and destroyed, other than that it must be done before the container completes a normal shutdown. A container is otherwise permitted to remove an idle instance from service at its own discretion, as long as it is prepared to fire up a new instance later if one is needed.

The specification does not define a mechanism for forcing a servlet instance to be unloaded. It having been unloaded, reclaiming its resources (mostly memory) is the job of the garbage collector, and when that happens is difficult to influence.

Overall, these are exactly the kind of details that you choose Java technology to avoid worrying about. If you insist on worrying about them anyway then look to the documentation of your chosen servlet container -- if there is a supported way to do what you are after then you will find it documented there. Such a thing would be container-specific.


-------------------------------------







Calling servlet's destroy method


As per the link http://www.xyzws.com/Servletfaq/when-is-destroy-of-servlets-called/20, one of the reason of calling destroy method is when the servlet hasn't got a request in a long time.

I was thinking there could be some pages that don't get called for a long time. So, does that mean destroy will be called and they will be no longer used?

Actually, I was asked this question in interview and he told me that destroy method will only be called when server is shut down.

Appreciate any help on this.



引用

When is destroy of servlets called?

The destroy() method is called by container before removing a servlet instance from service and gives servlet an opportunity to clean up any resources 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.

The destroy() and init() methods are called only once in a servlet's lifetime while the service() method may be called multiple times. The destory() will be called :

    - when the container shuts down or the application shuts down;
    - when the container decides that there is a shortage of memory;
    - when this servlet hasn't got a request in a long time.

After the servlet container calls this method, it will not call the service method again on this servlet.


As per the Doc


destroy():
   - 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.


Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. If the container needs to enable the servlet again, it must do so with a new instance of the servlet’s class.






---------
Answer:
---------

In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed to and will not destroy a java servlet.

The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues.

With respective to this question, the destroy() gets executed and then the servlet initialization gets completed.

destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected.

destroy() method generally contains code to free any resources like jdbc connection that will not be garbage collected.























分享到:
评论

相关推荐

    servlet基础与servlet容器模型

    Servlet的主要生命周期方法包括:`init()`(初始化)、`service()`(处理请求)和`destroy()`(销毁)。`init()`方法在Servlet实例化后首次调用,用于初始化Servlet;`service()`方法处理每个到来的请求;而`destroy...

    javax.servlet jar包---解决找不到javax.servlet.*等问题

    - 销毁:当Servlet不再需要时,容器调用`destroy()`方法释放资源,然后销毁Servlet实例。 6. **Servlet与Filter的协同工作** Filter可以在Servlet处理请求前和响应发送后执行额外操作,例如进行身份验证、日志...

    Servlet项目实践 实现学生信息系统的全部代码

    Servlet项目实践 实现学生信息系统的全部代码 一、Servlet简介  Servlet是sun公司提供的一门用于开发... ⑤WEB应用程序被停止或重新启动之前,Servlet引擎将卸载Servlet,并在卸载之前调用Servlet的destroy()方法。

    Servlet中文API文档 servlet

    1. **Servlet接口**:所有Servlet类必须实现javax.servlet.Servlet接口,该接口定义了Servlet的基本方法,如init()、service()和destroy()。其中,init()用于初始化Servlet,service()处理客户端请求,destroy()则在...

    用于servlet程序的开发的servlet-jar包

    当Servlet不再需要时,容器调用`destroy()`方法。 6. **部署描述符(web.xml)**:这是web应用的配置文件,定义了Servlet、过滤器和监听器等组件的映射、初始化参数和安全约束等。 7. **Servlet容器**:如Apache ...

    javaEE servlet-api

    最后,当服务器关闭或Servlet不再需要时,`destroy()`方法会被调用以释放资源。 Servlet API还提供了Filter的概念,允许开发者在请求到达Servlet之前和响应离开Servlet之后对其进行拦截和处理。`javax.servlet....

    servlet源码 servlet-api-src javax.servlet.Servlet源码

    当Web应用关闭或者Servlet不再需要时,容器会调用`destroy()`方法,让Servlet有机会释放资源。 `servlet-api-src`提供了Servlet API的源代码,这对于开发者来说是非常有价值的。通过阅读源码,我们可以理解Servlet ...

    servlet API 中文版

    Servlet API是Java服务器端编程的核心组件之一,主要用于构建Web应用程序。这个中文版文档可能包含对Servlet API的详细解释,便于中文使用者理解和学习。以下是对标题和描述中提到的一些关键知识点的详细说明: 1. ...

    Servlet基础知识总结

    Servlet是一种服务器端的小程序,是Java平台上的一个重要的技术之一,主要用于处理客户端的HTTP请求并生成动态网页。Servlet是在服务器端运行的,能够提供各种服务,比如处理表单数据、生成动态内容等。 #### 二、...

    serlvet 源码 servlet-src 源文件

    Servlet是Java Web应用程序的核心组件之一,它用于处理来自客户端(通常是Web浏览器)的请求并返回响应。`servlet-src`通常指的是Servlet的源代码,这对于理解Servlet的工作原理、学习如何编写Servlet以及进行自定义...

    servlet api servlet api

    - 销毁:当Servlet不再使用或者Web应用停止时,调用`destroy()`方法释放资源。 3. **Servlet API的主要接口和类**: - `Servlet`:所有Servlet的基类,定义了`init()`, `service()`, `destroy()`等方法。 - `...

    servlet 笔记

    4. **销毁**:当Servlet容器决定卸载Servlet时,会调用destroy()方法来释放Servlet所占用的资源。 #### 三、Servlet生命周期 Servlet的生命周期主要包括三个阶段:初始化、服务和销毁。 - **初始化**:通过调用...

    servlet线程安全问题

    Servlet 的多线程机制是建立在 Java 多线程机制之上的。Servlet 容器会自动使用线程池等技术来支持系统的运行。当客户端第一次请求某个 Servlet 时,Servlet 容器将会根据 web.xml 配置文件实例化这个 Servlet 类。...

    Servlet-Servlet生命周期

    4. **销毁**:当服务器关闭或者Servlet容器决定卸载Servlet时,会调用`destroy()`方法。在这个方法中,可以释放资源,如关闭数据库连接。 #### 四、Servlet生命周期中的方法详解 - **init()**:此方法在Servlet的...

    SERVLET基本原理

    4.销毁阶段:在这个阶段,SERVLET 容器会调用 SERVLET 的 destroy() 方法,以释放 SERVLET 占用的资源。 SERVLET 的生命周期是由 SERVLET 容器控制的,而不是由程序员控制的。程序员可以通过实现 javax.servlet....

    如何运行servlet(第一个servlet).doc

    Servlet 有四个基本方法:init、doGet、doPost 和 destroy。init 方法用于初始化 Servlet,doGet 和 doPost 方法用于处理 GET 和 POST 请求,destroy 方法用于释放资源。在我们的示例中,我们将编写一个简单的 ...

    servlet-api.jar 下载地址

    - `Servlet`: 这是所有Servlet的基类,定义了Servlet的基本方法,如`init()`(初始化Servlet)、`service()`(处理请求)和`destroy()`(销毁Servlet)。 - `GenericServlet`: 它实现了`Servlet`接口,提供了通用...

    Servlet+API+中文版

    - **销毁阶段**:当服务器关闭或者Servlet不再被需要时,容器会调用`destroy()`方法释放资源。 #### 四、通用Servlet类:`javax.servlet.GenericServlet` - **概述**:`GenericServlet`是一个抽象类,实现了`...

    servlet容器工作原理

    Servlet容器,如Tomcat,是Java Web应用的基础组成部分之一。本文旨在深入探讨Tomcat中的Servlet容器工作原理,重点解释Servlet容器如何加载和管理Servlet,以及如何处理HTTP请求。通过了解`javax.servlet.Servlet`...

Global site tag (gtag.js) - Google Analytics