我想写一个监控程序,用于监控在tomcat内存中,有多少个sevlet被装入到内存中。

解决方案 »

  1.   

    偶觉得你可以得到web.xml的内容
    看到里面servlet的配置
    但是有的servlet配了,不一定被启动了阿
      

  2.   

    想了想还是有可能的,大家看可不可以这样:首先写个 ServletCounter 类:public class ServletCounter {

        private static Object lock = new Object();
        private static ServletCounter instance = null;
        
        private int counter;
        
        private ServletCounter() {
         counter = 0;
        }
        
        public static ServletCounter getInstance() {
            if (instance==null) {
                synchronized(lock) {
                    if (instance == null) {
                        instance = new ServletCounter();                  
                    }
                }
            }
            return instance;
        }
        
        public synchronized int getCounter() {
            return counter;
        }    public synchronized int setCounter(int c) {
            counter = c;
            return counter;
        }    public synchronized int incCounter() {
            return (++counter);
        }
        
        public synchronized int decCounter() {
            return (--counter);
        }
    }
    然后在每个Servelet的init()方法中加入: ServletCounter.getInstance().incCounter();
    再在每个Servelet的destroy()方法中加入:ServletCounter.getInstance().decCounter();
    不就行了吗。
      

  3.   

    对于自己的写的可以使用ServletCounter类,但是,我们知道在tomcat启动的时候,它本身也有一些servlet被加载到内存中,这些servlet我们可以监控吗
      

  4.   


    现在想想,jsp 最终也会变成 servlet, 这些怎么监控?