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

边栏推荐
猜你喜欢

单调栈-503. 下一个更大元素 II

Base64编码简介

Dealing with duplicate data in Excel with xlwings

Minimap plug-in

单调栈-84. 柱状图中最大的矩形

Student educational administration management system of C # curriculum design

100 GIS practical application cases (78) - Multi compliance database design and data warehousing

Dotween plug-in

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

jupyter远程服务器配置以及服务器开机自启
随机推荐
Golang中删除字符串的最后一个字符
【Rust 笔记】11-实用特型
简易入手《SOM神经网络》的本质与原理
Golang 时间格式整理
【更新中】微信小程序学习笔记_3
Osgearth target selection
Initial unity
LinkedList set
Clion toolchains are not configured configure disable profile problem solving
Osgearth north arrow display
Conversion between string and int types in golang
梯度下降法求解BP神经网络的简单Demo
Minimap plug-in
UE4 source code reading_ Bone model and animation system_ Animation process
LinkList
Why can void * be a general pointer
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
producer consumer problem
Creation and content of mapnode -- osgearth rendering engine series (2)