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

边栏推荐
- Dealing with duplicate data in Excel with xlwings
- Exe file running window embedding QT window
- matlab神經網絡所有傳遞函數(激活函數)公式詳解
- Conversion between string and int types in golang
- Why can void * be a general pointer
- KunlunBase MeetUP 等您来!
- Eating fruit
- [audio and video] ijkplayer error code
- Use of ue5 QRcode plug-in
- 单调栈-84. 柱状图中最大的矩形
猜你喜欢

Unity learning notes

Get to know unity2 for the first time

Redis的数据结构

Dealing with duplicate data in Excel with xlwings

UE4 source code reading_ Bone model and animation system_ Animation compression

Abstract classes and interfaces

Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation

Display terrain database on osgearth ball

Simple demo of solving BP neural network by gradient descent method

详解sizeof、strlen、指针和数组等组合题
随机推荐
P1596 [USACO10OCT]Lake Counting S
Collection interface
简易入手《SOM神经网络》的本质与原理
C#课程设计之员工信息管理系统
Golang中删除字符串的最后一个字符
单调栈-84. 柱状图中最大的矩形
Development experience and experience
Creation and content of mapnode -- osgearth rendering engine series (2)
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Exe file running window embedding QT window
Unity4.3.1 engine source code compilation process
Map的实现类的顺序性
Kunlunbase meetup is waiting for you!
producer consumer problem
【K&R】中文第二版 个人题解 Chapter1
Get to know unity2 for the first time
Notes on understanding applets 2022/7/3
Golang url的编码和解码
Classes and objects
Markdown learning