003_Servlet生命周期

1. 创建一个LifeCycle的Web工程

2. 在WebContent下创建一个index.html

3. 编写index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Life Cycle</title>
	</head>
	<body>
		<h1>Life Cycle</h1>
	</body>
</html>

4. 创建LifeCycle.java

5. 编写LifeCycle.java

package com.lywgames.myservlet;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class LifeCycle implements Servlet {
	public LifeCycle() {
		System.out.println("LifeCycle");
	}
@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("init");
	}
@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println("service");
	}
     @Override
	public void destroy() {
		System.out.println("destroy");
	}
     @Override
	public ServletConfig getServletConfig() {
		return null;
	}
     @Override
	public String getServletInfo() {
		return null;
	}
}

6. 在web.xml里配置servlet

7. 在浏览器地址栏输入:http://localhost:8080/LifeCycle/LC.action运行几次

8. Servlet的生命周期: 从创建到销毁的一段时间。

9. Servlet生命周期方法: 从创建到销毁, 所调用的那些方法。主要有: 构造方法、init()、service()和destroy()。

10. init方法: 在创建该servlet的实例时, 就执行该方法。一个servlet只会初始化一次, init方法只会执行一次。默认情况下是: 初次访问该servlet, 才会创建实例。

11. service方法: 只要客户端来了一个请求, 那么就执行这个方法了。该方法可以被执行很多次。一次请求, 对应一次service方法的调用。

12. destroy方法: servlet销毁的时候, 就会执行该方法。

12.1. 该项目从tomcat的里面移除。

12.2. 正常关闭tomcat就会执行shutdown.bat。

12.3. Eclipse里面stop the server。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值