当前位置:网站首页>Servlet的生命周期
Servlet的生命周期
2022-07-03 08:35:00 【緈福的街口】
Servlet生命周期
1、装载-web.xml
<!-- 声明Servlet -->
<servlet>
<!-- servlet的别名 -->
<servlet-name>first</servlet-name>
<!-- 敏感信息,需用别名 -->
<servlet-class>com.imooc.servlet.FirstServlet</servlet-class>
</servlet>
<!-- 将Servlet与URL绑定 -->
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/hi</url-pattern>
</servlet-mapping>
装载web.xml的时候不会创建FirstServlet对象
2、创建-构造函数
public FirstServlet() {
System.out.println("正在创建FirstServlet对象");
}
3、初始化-init()
public void init(ServletConfig config) throws ServletException {
System.out.println("正在初始化FirstServlet对象");
}
4、提供服务-service()
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 接收请求发来的参数
String name = request.getParameter("name");
String html = "<h1 style='color:red'>hi," + name + "!</h1><hr/>";
System.out.println("返回给浏览器的响应数据为:" + html);
PrintWriter out = response.getWriter();
out.println(html);//将html发送回浏览器
}
- 在进行访问service()方法的时候创建FirstServlet对象


- 多次访问,在全局中只有一个FirstServlet对象

5、销毁-destory()
public void destroy() {
System.out.println("正在销毁FirstServlet对象");
}
- 调整代码之后会销毁FirstServlet对象,再次访问时才会创建FirstServlet对象

边栏推荐
- 【Rust 笔记】08-枚举与模式
- [cloud native] introduction and use of feign of microservices
- 【K&R】中文第二版 个人题解 Chapter1
- 【Rust 笔记】12-闭包
- Advanced OSG collision detection
- Animation_ IK overview
- Downward compatibility and upward compatibility
- jupyter远程服务器配置以及服务器开机自启
- Location of package cache downloaded by unity packagemanager
- 【更新中】微信小程序学习笔记_3
猜你喜欢

Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template

详解sizeof、strlen、指针和数组等组合题

UE4 source code reading_ Bone model and animation system_ Animation node

Display terrain database on osgearth ball

Explain sizeof, strlen, pointer, array and other combination questions in detail
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données

Simply start with the essence and principle of SOM neural network

MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板

简易入手《SOM神经网络》的本质与原理
随机推荐
Golang 时间格式整理
【Rust 笔记】13-迭代器(上)
【K&R】中文第二版 个人题解 Chapter1
swagger文档配置
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Introduction to hexadecimal coding
Exe file running window embedding QT window
LinkList
Redis data structure
【云原生】微服务之Feign的介绍与使用
UE4 source code reading_ Bone model and animation system_ Animation compression
Unity Editor Extension - drag and drop
Swagger document configuration
LinkedList set
Development material set
go 解析身份证
【Rust 笔记】10-操作符重载
Osganimation library parsing
Golang time format sorting
UE4 source code reading_ Bone model and animation system_ Animation node