- 浏览: 1115914 次
文章分类
- 全部博客 (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学习
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:
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:
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.
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.
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:
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.
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.
发表评论
-
javaWeb session失效时间设置
2018-04-25 14:19 2017session失效时间设置 session失效时间设 ... -
JSP 表达式 VS EL 表达式 用法比较
2017-06-21 06:03 1055应用场景: 根据访问路径URL,判断当前用户选择使用的语言。转 ... -
JSP之 @include VS jsp:include
2017-06-17 01:39 1419第一篇 对于可以重复使用的一段代码, 1、使用 <%@ ... -
JavaEE之(Servlet+Filter)环境搭建
2017-04-01 14:15 1060初学 Java Web 开发,请远 ... -
Servlet之单例与线程安全
2017-02-06 13:04 4442一、Servlet 是单例吗 不 ... -
Servlet之 destroy
2016-11-29 22:04 794只听说过 Servlet 何时 init(),何时 servi ... -
Servlet之JSP_03 JSTL/EL 表达式
2016-09-16 16:13 2319准备工作:在JSP 中启用 EL 表达式 <%@ ... -
Servlet之JSP_02初探
2016-09-15 22:37 845一、被编译后的JSP 1、写一个JSP文件:index.js ... -
Servlet之JSP_01概述
2016-09-15 20:42 1167一、什么是JSP JSP (Java Server Page ... -
Servlet之Servlet API
2016-09-13 19:10 1617一、在哪里 Java Servlet ... -
在一个JSP页面中,操作数据库
2013-03-11 19:02 2789下面的代码是在一个jsp页面中实现数据库的操作。 也是为了加深 ... -
Servlet之Filter及FilterChain的使用详解
2013-01-07 20:06 2172在 JavaEE 中没有 Interceptor(拦截器)的概 ... -
out.print和out.write
2012-12-30 11:42 19440问题: 这是一个JSP页面: <%@ page l ... -
等幂操作——get方法与post方法
2012-12-23 20:13 1759幂等操作: 指的是对于同一数据,不论对其进行任何次幂操作,总 ... -
如何将jsp中<input>设为只读
2012-12-13 10:49 23339将一个input变为只读,可以使用 readonly 属性 和 ... -
Request的 getAttribute 和 getParameter
2012-12-03 19:57 1284getParameter 返回的是String, 用于 ...
相关推荐
### Servlet生命周期详解 #### 一、引言 在Java Web开发中,Servlet作为一种重要的技术,被广泛应用于构建动态网页和处理客户端请求。了解Servlet的生命周期对于深入理解和掌握Servlet的工作机制至关重要。本文将...
初始化阶段是整个Servlet生命周期的第一个关键步骤,它标志着Servlet的开始。此阶段主要由以下步骤组成: 1. **Servlet容器加载Servlet类**:Servlet容器负责加载Servlet类,并将.Class文件的数据读入内存。这一...
### SERVLET生命周期与JSP生命周期比较 #### 一、引言 在现代Web开发中,Servlet和JSP是两种非常重要的技术,它们都属于Java EE平台的一部分,主要用于构建动态Web应用程序。这两种技术各有特点,但又紧密相关,...
### Servlet 生命周期演示代码详解 #### 一、Servlet 生命周期概述 在深入分析代码之前,我们先来了解一下 Servlet 的生命周期。Servlet 的生命周期主要包括三个阶段:初始化 (`init` 方法)、请求处理 (`service` ...
**Servlet生命周期** Servlet的生命周期可以分为三个主要阶段:初始化、服务和销毁。 1. **初始化阶段**: - 当Servlet首次被请求或者在web应用启动时,容器(如Tomcat)会加载Servlet类,并调用`init()`方法进行...
201340206_蔡晓双_servlet生命周期图.vsdx
Servlet生命周期是Java Web开发中一个关键的概念,它描述了Servlet从创建到销毁的整个过程,这个过程由Servlet容器(如Tomcat)进行管理。Servlet生命周期主要分为三个阶段:初始化阶段、运行阶段和销毁阶段。 1. ...
关于对Servlet声明周期的图例示意!
servlet生命周期详细图解,矢量图。 详细的解释请参考本人博客:http://blog.csdn.net/dwyers/article/details/38435949
### Servlet 生命周期详解 #### 一、概述 Servlet 是 Java Web 开发的核心技术之一,用于处理客户端的 HTTP 请求并返回响应。了解 Servlet 的生命周期对于更好地控制和优化 Web 应用程序至关重要。Servlet 的生命...
java servlet生命周期 java servlet生命周期
Servlet生命周期和模板设计模式是Java Web开发中的两个关键概念,它们在构建动态Web应用程序时起着至关重要的作用。 首先,让我们深入理解Servlet的生命周期。Servlet是Java编程语言中的一种接口,用于扩展服务器的...
在Servlet生命周期中,init()方法只会被调用一次,service()方法会在每次用户请求时被调用,destroy()方法只会被调用一次,在Servlet生命周期结束时被调用。Servlet生命周期的正确实现是保证Servlet正确运行的关键。
#### 三、Servlet生命周期详解 ##### 实例化 - **按需创建**:默认情况下,当第一次HTTP请求到达时,容器会创建Servlet的实例。 - **预加载**:通过在`web.xml`中配置`<load-on-startup>`标签,可以在应用启动时...
Java WEB 篇七 Servlet 生命周期
Servlet的生命周期指的是从创建到销毁的过程。这个过程包括以下几个阶段: 1. 初始化阶段:通过调用init()方法来完成,这个方法在Servlet创建后被调用一次。 2. 处理请求阶段:通过调用service()方法处理客户端请求...