- 浏览: 1111096 次
文章分类
- 全部博客 (379)
- S2SH (16)
- stuts2 (0)
- java语言 (81)
- JSP (17)
- <html>元素 (11)
- javaweb (4)
- web容器 (3)
- ext (23)
- javaScript (48)
- ant (1)
- liferay (1)
- sql (9)
- css (42)
- 浏览器设置 (3)
- office_world (1)
- eclipse (4)
- 其它 (28)
- 操作系统 (5)
- android (6)
- Struts2 (11)
- RegEx (3)
- mysql (5)
- BigDATA (1)
- Node.js (1)
- Algorithm (10)
- Apache Spark (1)
- 数据库 (5)
- linux (2)
- git (1)
- Adobe (3)
- java语言,WebSocket (1)
- Maven (3)
- SHELL (1)
- XML (2)
- 数学 (2)
- Python (2)
- Java_mysql (1)
- ReactJS (6)
- 养生 (4)
- Docker (1)
- Protocols (3)
- java8 (2)
- 书籍 (1)
- Gradle (2)
- AngularJS (5)
- SpringMVC (2)
- SOAP (1)
- BootstrapCSS (1)
- HTTP协议 (1)
- OAuth2 (1)
最新评论
-
Lixh1986:
Java并发编程:自己动手写一把可重入锁https://blo ...
Java之多线程之Lock与Condition -
Lixh1986:
http://win.51apps.com.cn/https: ...
temp -
ztwsl:
不错,支持很好
HttpServletRequest和ServletRequest的区别 -
guodongkai:
谢谢您能将知识精华汇编总结,让初学者们从原理中学会和提高。
javaScript之function定义 -
kangwen23:
谢谢了,顶顶
struts2中的ValueStack学习
只听说过 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.
____________________________________________________________________
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.
发表评论
-
javaWeb session失效时间设置
2018-04-25 14:19 1988session失效时间设置 session失效时间设 ... -
JSP 表达式 VS EL 表达式 用法比较
2017-06-21 06:03 1051应用场景: 根据访问路径URL,判断当前用户选择使用的语言。转 ... -
JSP之 @include VS jsp:include
2017-06-17 01:39 1416第一篇 对于可以重复使用的一段代码, 1、使用 <%@ ... -
JavaEE之(Servlet+Filter)环境搭建
2017-04-01 14:15 1035初学 Java Web 开发,请远 ... -
Servlet之单例与线程安全
2017-02-06 13:04 4439一、Servlet 是单例吗 不 ... -
Servlet之JSP_03 JSTL/EL 表达式
2016-09-16 16:13 2315准备工作:在JSP 中启用 EL 表达式 <%@ ... -
Servlet之JSP_02初探
2016-09-15 22:37 842一、被编译后的JSP 1、写一个JSP文件:index.js ... -
Servlet之JSP_01概述
2016-09-15 20:42 1162一、什么是JSP JSP (Java Server Page ... -
Servlet之Servlet API
2016-09-13 19:10 1611一、在哪里 Java Servlet ... -
Servlet 的生命周期图
2014-12-31 03:18 950A servlet life cycle can be def ... -
在一个JSP页面中,操作数据库
2013-03-11 19:02 2784下面的代码是在一个jsp页面中实现数据库的操作。 也是为了加深 ... -
Servlet之Filter及FilterChain的使用详解
2013-01-07 20:06 2161在 JavaEE 中没有 Interceptor(拦截器)的概 ... -
out.print和out.write
2012-12-30 11:42 19427问题: 这是一个JSP页面: <%@ page l ... -
等幂操作——get方法与post方法
2012-12-23 20:13 1750幂等操作: 指的是对于同一数据,不论对其进行任何次幂操作,总 ... -
如何将jsp中<input>设为只读
2012-12-13 10:49 23331将一个input变为只读,可以使用 readonly 属性 和 ... -
Request的 getAttribute 和 getParameter
2012-12-03 19:57 1280getParameter 返回的是String, 用于 ...
相关推荐
Servlet的主要生命周期方法包括:`init()`(初始化)、`service()`(处理请求)和`destroy()`(销毁)。`init()`方法在Servlet实例化后首次调用,用于初始化Servlet;`service()`方法处理每个到来的请求;而`destroy...
- 销毁:当Servlet不再需要时,容器调用`destroy()`方法释放资源,然后销毁Servlet实例。 6. **Servlet与Filter的协同工作** Filter可以在Servlet处理请求前和响应发送后执行额外操作,例如进行身份验证、日志...
Servlet项目实践 实现学生信息系统的全部代码 一、Servlet简介 Servlet是sun公司提供的一门用于开发... ⑤WEB应用程序被停止或重新启动之前,Servlet引擎将卸载Servlet,并在卸载之前调用Servlet的destroy()方法。
1. **Servlet接口**:所有Servlet类必须实现javax.servlet.Servlet接口,该接口定义了Servlet的基本方法,如init()、service()和destroy()。其中,init()用于初始化Servlet,service()处理客户端请求,destroy()则在...
当Servlet不再需要时,容器调用`destroy()`方法。 6. **部署描述符(web.xml)**:这是web应用的配置文件,定义了Servlet、过滤器和监听器等组件的映射、初始化参数和安全约束等。 7. **Servlet容器**:如Apache ...
最后,当服务器关闭或Servlet不再需要时,`destroy()`方法会被调用以释放资源。 Servlet API还提供了Filter的概念,允许开发者在请求到达Servlet之前和响应离开Servlet之后对其进行拦截和处理。`javax.servlet....
当Web应用关闭或者Servlet不再需要时,容器会调用`destroy()`方法,让Servlet有机会释放资源。 `servlet-api-src`提供了Servlet API的源代码,这对于开发者来说是非常有价值的。通过阅读源码,我们可以理解Servlet ...
Servlet接口是所有Servlet的基础,它定义了Servlet的主要方法,如`init()`, `service()`, `destroy()`。在Servlet的生命周期中,`init()`方法在Servlet实例化后首次被调用,用于初始化Servlet;`service()`方法处理...
Servlet API是Java服务器端编程的核心组件之一,主要用于构建Web应用程序。这个中文版文档可能包含对Servlet API的详细解释,便于中文使用者理解和学习。以下是对标题和描述中提到的一些关键知识点的详细说明: 1. ...
Servlet是一种服务器端的小程序,是Java平台上的一个重要的技术之一,主要用于处理客户端的HTTP请求并生成动态网页。Servlet是在服务器端运行的,能够提供各种服务,比如处理表单数据、生成动态内容等。 #### 二、...
Servlet是Java Web应用程序的核心组件之一,它用于处理来自客户端(通常是Web浏览器)的请求并返回响应。`servlet-src`通常指的是Servlet的源代码,这对于理解Servlet的工作原理、学习如何编写Servlet以及进行自定义...
- 销毁:当Servlet不再使用或者Web应用停止时,调用`destroy()`方法释放资源。 3. **Servlet API的主要接口和类**: - `Servlet`:所有Servlet的基类,定义了`init()`, `service()`, `destroy()`等方法。 - `...
4. **销毁**:当Servlet容器决定卸载Servlet时,会调用destroy()方法来释放Servlet所占用的资源。 #### 三、Servlet生命周期 Servlet的生命周期主要包括三个阶段:初始化、服务和销毁。 - **初始化**:通过调用...
Servlet 的多线程机制是建立在 Java 多线程机制之上的。Servlet 容器会自动使用线程池等技术来支持系统的运行。当客户端第一次请求某个 Servlet 时,Servlet 容器将会根据 web.xml 配置文件实例化这个 Servlet 类。...
4. **销毁**:当服务器关闭或者Servlet容器决定卸载Servlet时,会调用`destroy()`方法。在这个方法中,可以释放资源,如关闭数据库连接。 #### 四、Servlet生命周期中的方法详解 - **init()**:此方法在Servlet的...
4.销毁阶段:在这个阶段,SERVLET 容器会调用 SERVLET 的 destroy() 方法,以释放 SERVLET 占用的资源。 SERVLET 的生命周期是由 SERVLET 容器控制的,而不是由程序员控制的。程序员可以通过实现 javax.servlet....
Servlet 有四个基本方法:init、doGet、doPost 和 destroy。init 方法用于初始化 Servlet,doGet 和 doPost 方法用于处理 GET 和 POST 请求,destroy 方法用于释放资源。在我们的示例中,我们将编写一个简单的 ...
- `Servlet`: 这是所有Servlet的基类,定义了Servlet的基本方法,如`init()`(初始化Servlet)、`service()`(处理请求)和`destroy()`(销毁Servlet)。 - `GenericServlet`: 它实现了`Servlet`接口,提供了通用...
- **销毁阶段**:当服务器关闭或者Servlet不再被需要时,容器会调用`destroy()`方法释放资源。 #### 四、通用Servlet类:`javax.servlet.GenericServlet` - **概述**:`GenericServlet`是一个抽象类,实现了`...