用的是tomcat5.5,是不是在关闭tomcat时候自动销毁 ? 为什么我自己的类实现了
ServletContextListener事件检听器,可是他却没有调用contextDestroyed方法呢,
还是需要自己在程序中销毁 ? 请教高手
注:contextInitialized已经在tomcat启动时自动调用了,可是destroy却不知何时调

解决方案 »

  1.   

    在tomcat退出的时候会调用,用来通知ServletContext该销毁了你的可能是别的原因,最好有代码,不然不好说。
      

  2.   

    import java.io.*;
    import java.util.*;
    import bookBean.*;
    import api.*;
    import personBean.*;
    import javax.servlet.*;
    import javax.servlet.http.*;public class ContextConfig implements ServletContextListener
    {
    public void contextInitialized(ServletContextEvent sce)
    {
    ServletContext app = sce.getServletContext();
    InputStream ips = app.getResourceAsStream("/WEB-INF/records/config.properties");
    Properties props = new Properties();
    try
    {
    props.load(ips);
    }
    catch (Exception e)
    {
    System.out.println(e);
    }
    String book = props.getProperty("book"),
       person = props.getProperty("person"),
       record = props.getProperty("record"),
       admin = props.getProperty("admin"),
       name = props.getProperty("name"),
       password = props.getProperty("password");
    Manageable manage = new Manage();
    manage.load(book,person,record,admin);
    boolean b = manage.setAdminister(name,password);
    System.out.println(b ? "添加成功,帐号:" + name + "密码:" + password:"添加失败");
    app.setAttribute("manage",manage);
    }
    public void contextDestroyed(ServletContextEvent sce) 
    {
    ServletContext app = sce.getServletContext();
    InputStream ips = app.getResourceAsStream("/WEB-INF/records/config.properties");
    Properties props = new Properties();
    try
    {
    props.load(ips);
    }
    catch (Exception e)
    {
    System.out.println(e);
    }
    String book = props.getProperty("book"),
       person = props.getProperty("person"),
       record = props.getProperty("record"),
       admin = props.getProperty("admin");
    Manageable manage = (Manageable)app.getAttribute("manage");
    manage.save(book,person,record,admin);
    }
    }这是代码,目的是在结束时 取出context对象中的manage对象并调用其save方法将资料保存,已测试过save方法是正确的
      

  3.   

    你在contextDestroyed里面加上一个System.out.println("aaaaaa");看看有没有输出你在什么情况下发现这个方法没有被调用的呢?
    contextDestroyed必须是合法关闭才可以,也就是说杀进程是不行的。
    在调试环境下设置断点看看
      

  4.   

    你是否已经在web.xml中加入<listener>标记,不加入的话,Tomcat并不知道需要通知你
      

  5.   

    已经加入了listener标记了.
    谢谢大家了,我是非法关闭了刚才.顺便问一下,tomcat合法关闭除了ctrl+c还有什么办法?
    点关闭按钮代码都不执行.
      

  6.   

    可以按关闭按钮,不过你的tomcat是什么版本的。干脆搞到服务里面算了