- 浏览: 1087602 次
文章分类
- 全部博客 (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 1898session失效时间设置 session失效时间设 ... -
JSP 表达式 VS EL 表达式 用法比较
2017-06-21 06:03 999应用场景: 根据访问路径URL,判断当前用户选择使用的语言。转 ... -
JSP之 @include VS jsp:include
2017-06-17 01:39 1360第一篇 对于可以重复使用的一段代码, 1、使用 <%@ ... -
JavaEE之(Servlet+Filter)环境搭建
2017-04-01 14:15 959初学 Java Web 开发,请远 ... -
Servlet之单例与线程安全
2017-02-06 13:04 4364一、Servlet 是单例吗 不 ... -
Servlet之 destroy
2016-11-29 22:04 727只听说过 Servlet 何时 init(),何时 servi ... -
Servlet之JSP_03 JSTL/EL 表达式
2016-09-16 16:13 2267准备工作:在JSP 中启用 EL 表达式 <%@ ... -
Servlet之JSP_02初探
2016-09-15 22:37 794一、被编译后的JSP 1、写一个JSP文件:index.js ... -
Servlet之JSP_01概述
2016-09-15 20:42 1100一、什么是JSP JSP (Java Server Page ... -
Servlet之Servlet API
2016-09-13 19:10 1574一、在哪里 Java Servlet ... -
在一个JSP页面中,操作数据库
2013-03-11 19:02 2747下面的代码是在一个jsp页面中实现数据库的操作。 也是为了加深 ... -
Servlet之Filter及FilterChain的使用详解
2013-01-07 20:06 2087在 JavaEE 中没有 Interceptor(拦截器)的概 ... -
out.print和out.write
2012-12-30 11:42 19368问题: 这是一个JSP页面: <%@ page l ... -
等幂操作——get方法与post方法
2012-12-23 20:13 1702幂等操作: 指的是对于同一数据,不论对其进行任何次幂操作,总 ... -
如何将jsp中<input>设为只读
2012-12-13 10:49 23251将一个input变为只读,可以使用 readonly 属性 和 ... -
Request的 getAttribute 和 getParameter
2012-12-03 19:57 1226getParameter 返回的是String, 用于 ...
相关推荐
SERVLET生命周期与JSP生命周期比较
servlet生命周期详解
servlet生命周期演示,便于理解servlet工作流程。
201340206_蔡晓双_servlet生命周期图.vsdx
Servlet生命周期与工作原理Servlet使用
关于对Servlet声明周期的图例示意!
servlet生命周期详细图解,矢量图。 详细的解释请参考本人博客:http://blog.csdn.net/dwyers/article/details/38435949
servlet生命周期和模版设计模式,包括代码以及相关文档
Servlet生命周期,web开发必须掌握到知识点,其中生命周期也是需要掌握的
山寨qq项目1.登录界面教你学会制作qq登录及后台运行。
达内学习笔记,servlet生命周期,十分详细,保证一看就懂
从三个阶段来描述servlet的生命周期:初始化阶段、运行阶段和销毁阶段。
Java WEB 篇七 Servlet 生命周期
Servlet技术 两条主线 1、HTTP协议 2、Servlet生命周期
注:本示例中有5章图片,但因为上传比较麻烦,所以就放在附件中 博文链接:https://lvp.iteye.com/blog/234610
Servlet 生命周期
Servlet生命周期就是servlet从创建到销毁的过程,包括加载和实例化,初始化,服务,和销毁4步。 当客户第一次请求时,首先判断是否存在servlet对象,第一次请求时不存在,则由tomcat创建对象,调用init()方法对...
Servlet运行在Servlet容器中,其生命周期由容器来管理。Servlet的生命周期通过javax.servlet.Servlet接口中的init()、service()和destroy()方法来表示。