当前位置:网站首页>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对象

边栏推荐
- Animation_ IK overview
- 二进制转十进制,十进制转二进制
- Gradle's method of dynamically modifying APK package name
- 了解小程序的笔记 2022/7/3
- Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
- UE4 plug in development
- [cloud native] introduction and use of feign of microservices
- Some understandings of 3dfiles
- Dealing with duplicate data in Excel with xlwings
- OpenGL learning notes
猜你喜欢

Chocolate installation

Message queue for interprocess communication

Simple demo of solving BP neural network by gradient descent method

Markdown learning

animation

Base64和Base64URL

matlab神经网络所有传递函数(激活函数)公式详解
![[updating] wechat applet learning notes_ three](/img/05/958b8d62d3a42b38ca1a2d8631a7f8.png)
[updating] wechat applet learning notes_ three
![[concurrent programming] consistency hash](/img/5e/3d0a52caa8ca489a6e6267274bbb39.jpg)
[concurrent programming] consistency hash

【更新中】微信小程序学习笔记_3
随机推荐
【更新中】微信小程序学习笔记_3
Golang 时间格式整理
Conversion between string and int types in golang
Simply start with the essence and principle of SOM neural network
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Unity4.3.1 engine source code compilation process
redis集群系列四
Explain sizeof, strlen, pointer, array and other combination questions in detail
Unity interactive water ripple post-treatment
Osgconv tool usage
ArrayList
Data analysis exercises
Base64 and base64url
Cesium for unreal quick start - simple scenario configuration
Message queue for interprocess communication
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
【Rust 笔记】09-特型与泛型
Student educational administration management system of C # curriculum design
Osgearth north arrow display
Gradle's method of dynamically modifying APK package name