JavaWeb-Servlet学习日记


destory 方法被调用后,servlet 被销毁,但是并没有立即被回收,再次请求时,并没有重新初始化。

代码示例:

private String message;

@Override
public void init() throws ServletException {
    message = "Hello World , Nect To Meet You: " + System.currentTimeMillis();
    System.out.println("servlet初始化……");
    super.init();
}

@Override
public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.write("<h1>" + message + "</h1>");
    destroy();
}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO Auto-generated method stub
    super.doPost(req, resp);
}

@Override
public void destroy() {
    System.out.println("servlet销毁!");
    super.destroy();
}

控制台打印:

servlet初始化……
servlet销毁!
2020-7-18 19:48:52 org.apache.catalina.core.StandardContext reload
信息: Reloading Context with name [/myServlet] has started
servlet销毁!
2020-7-18 19:48:52 org.apache.catalina.core.StandardContext reload
信息: Reloading Context with name [/myServlet] is completed
servlet初始化……
servlet销毁!
servlet销毁!
servlet销毁!
servlet销毁!
servlet销毁!
servlet销毁!
servlet销毁!

声明:混世大魔王|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - JavaWeb-Servlet学习日记


好好休息,别太劳累