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

边栏推荐
- 单调栈-42. 接雨水
- Graphics_ Learnopongl learning notes
- Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
- 【Rust 笔记】07-结构体
- Redis的数据结构
- Kunlunbase meetup is waiting for you!
- KunlunBase MeetUP 等您来!
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
- Abstract classes and interfaces
- 796 · 开锁
猜你喜欢

Dealing with duplicate data in Excel with xlwings

Mall management system of database application technology course design

【Rust笔记】02-所有权

Installation of PHP FPM software +openresty cache construction

Three characteristics

Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial

Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB

二进制转十进制,十进制转二进制

數據庫應用技術課程設計之商城管理系統

Kunlunbase meetup is waiting for you!
随机推荐
单调栈-84. 柱状图中最大的矩形
【Rust笔记】05-错误处理
使用base64编码传图片
[redis] redis persistent RDB vs AOF (source code)
Development material set
Creation and content of mapnode -- osgearth rendering engine series (2)
Find the intersection of line segments
P1596 [USACO10OCT]Lake Counting S
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
【Rust 笔记】11-实用特型
Unity Editor Extension - drag and drop
Delete the last character of the string in golang
Graphics_ Games101/202 learning notes
Osgearth north arrow display
Transmit pictures with Base64 encoding
Conversion between string and int types in golang
Dotween plug-in
单调栈-42. 接雨水
Markdown directory generation
Basic operation and process control 2