`

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简介  Servlet是sun公司提供的一门用于开发... ⑤WEB应用程序被停止或重新启动之前,Servlet引擎将卸载Servlet,并在卸载之前调用Servlet的destroy()方法。

    JSP/Servlet Java面试逻辑题

    这个生存期由javax.servlet.Servlet接口的init,service和destroy方法表达。 2、servlet的生命周期 答: web容器加载servlet,生命周期开始。通过调用servlet的init()方法进行servlet的初始化。 通过调用service...

    servlet temple

     当服务器不再需要 Servlet, 或重新装入 Servlet 的新实例时,服务器会调用 Servlet 的 destroy() 方法。  3. Java Servlet API  Java Servlet 开发工具(JSDK)提供了多个软件包,在编写 Servlet 时需要用到...

    Servlet过滤器使用

    Servlet过滤器,主要配置了中文乱码及未登陆验证过滤器。 代码简单明了,易学。 过滤器实现类 ... c、destroy(): Servlet容器在销毁过滤器实例前调用该方法,这个方法中可以释放Servlet过滤器占用的资源。

    servlet2.4doc

    destroy() - Method in interface javax.servlet.Servlet Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. destroy() - Method in class javax....

    Servlet API 帮助文档

    servlet抽象集是javax.servlet.Servlet接口,它规定了必须由Servlet类实现由servlet引擎识别和管理的方法集。  Servlet接口的基本目标是提供生命期方法init()、service()和destroy()方法。

    java-servlet-api.doc

    为了完成以上工作,引擎会调用Servlet的destroy()方法。 在卸载一个Servlet之前,Servlet引擎必须等待所有的service()方法完成或超时结束(Servlet引擎会对超时作出定义)。当一个Servlet被卸载时,引擎将不能给...

    Servlet的生命周期

    Servlet运行在Servlet容器中,其生命周期由容器来管理。Servlet的生命周期通过javax.servlet.Servlet接口中的init()、service()和destroy()方法来表示。

    servlet +API+实例

    Servlet有良好的生存期的定义,包括如何加载、实例化、初始化、处理客户端请求以及如何被移除。这个生存期由javax.Servlet.Servlet接口的init,service和destroy方法表达。

    Servlet生命周期与工作原理

    Servlet生命周期与工作原理 Servlet生命周期分为三个阶段:  1,初始化阶段 调用init()方法  2,响应客户请求阶段 调用service()方法  3,终止阶段 调用destroy()方法 Servlet初始化阶段:

    servlet技术功能全套

    servlet程序设计与支持 Servlet生命周期分为三个阶段:  1,初始化阶段 调用init()方法  2,响应客户请求阶段 调用service()方法  3,终止阶段 调用destroy()方法

    JSP和Servlet面试题

    方法初始化servlet,再根据不同请求调用doGet或doPost方法,最后再通过destroy()方法进行销毁。 2.Get和Post的区别 在页面提交数据时,get的数据大小有限制4k,post没有限制,get请求提交的数据会在地址栏显示,...

    servletAPI中文版(TXT格式,自己翻译)

    Servlet接口的基本目标是提供生命期方法init()、service()和destroy()方法。 servlet接口中的方法 void init(ServletConfit config)throws ServletException 在servlet被载入后和实施服务前由servlet引擎进行?次性...

    servlet资源

    当提交信息时,它们还指定服务器应执行哪一个Servlet(或其它的程序)。 HttpServlet 类包含 init()、destroy()、service() 等方法。其中 init() 和 destroy() 方法是继承的(HttpServlet扩展至GenericServlet)。所有...

    Tomcat服务器配置、启动分析、Servlet文件配置

    内容包括了Tomcat服务器配置、启动分析、Servlet文件配置等tomcat的基础应用配置。

    Servlet查询数据库案例--Query(java源码)

    public void destroy() { try { db.close(); } // Try to close the connection catch (SQLException e) {} // Ignore errors; at least we tried! } public void doGet(HttpServletRequest request, ...

    Servlet api

    javax.servlet.Servlet接口 servlet抽象集是javax.servlet.Servlet接口,它规定了必须由Servlet类实现由servlet引擎...Servlet接口的基本目标是提供生命期方法init()、service()和destroy()方法。 servlet接口中的方法

    tomcat中Servlet的工作机制详细介绍

    在研究Servlet在tomcat中的工作机制前必须先看看Servlet规范的一些重要的相关规定,规范提供了一个Servlet接口,接口中包含的重要方法是init、service、destroy等方法,Servlet在初始化时要调用init方法,在销毁时要...

    Servlet生命周期

    3.Servlet 通过调用 destroy() 方法终止(结束)。 4.最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。 现在让我们详细讨论生命周期的方法。 操作实例: import java.io.IOException; import javax.se

    java servlet 监听器

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { MyRequest myRequest = null; HttpServletRequest request2 = ...

Global site tag (gtag.js) - Google Analytics