对象的生命周期指一个对象从被创建到被销毁的整个过程
- import javax.servlet.*;
- import javax.servlet.annotation.WebServlet;
- import java.io.IOException;
- @WebServlet(urlPatterns = "/demo",loadOnStartup = 10)
- public class ServletDemo implements Servlet {
-
- /**
- * 提供服务
- * 1.调用时机:每次Servlet被访问的时候
- * 2.调用次数:无数次
- * @param servletRequest
- * @param servletResponse
- * @throws ServletException
- * @throws IOException
- */
- public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
- System.out.println("动态Web资源开发技术");
- }
- /**
- * 完成初始化的方法
- * 1.调用时机:默认情况下,Servlet被第一次访问的时候
- * *loadOnStartup:
- * 2.调用次数:1次
- * @param servletConfig
- * @throws ServletException
- */
- public void init(ServletConfig servletConfig) throws ServletException {
- System.out.println("init");
- }
-
- /**
- * 获取ServletConfig对象
- * @return
- */
- public ServletConfig getServletConfig() {
- return null;
- }
-
- /**
- * 获取Servlet信息
- * @return
- */
- public String getServletInfo() {
- return null;
- }
-
- /**
- * 销毁方法
- * 1.调用时机:内存释放或服务器关闭的时候,Servlet(小服务器程序)对象会被销毁
- * 2.调用次数:1次
- */
- public void destroy() {
- System.out.println("xiaohui1");
- }
- }