Servlet全生命周期
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
@WebServlet(
name = "ExampleServlet",
urlPatterns = {"/example"},
description = "An example Servlet",
initParams = {
@WebInitParam(name = "param1", value = "value1"),
@WebInitParam(name = "param2", value = "value2")
},
asyncSupported = true
)
public class ExampleServlet extends HttpServlet {
@Override
public void init() throws ServletException {
// 在Servlet初始化时调用,可以进行一些初始化工作
super.init();
String param1 = getInitParameter("param1");
String param2 = getInitParameter("param2");
System.out.println("Servlet初始化... param1: " + param1 + ", param2: " + param2);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 处理GET请求的主要方法
System.out.println("处理GET请求...");
response.setContentType("text/plain");
response.getWriter().write("Hello from ExampleServlet!");
}
@Override
public void destroy() {
// 在Servlet销毁时调用,可以进行一些清理工作
System.out.println("Servlet销毁...");
super.destroy();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45