如何测量HTTP会话大小



在基于servlet的应用程序中,是否有一些有效且准确的方法来跟踪特定会话的大小?

Java没有像C那样的sizeof()方法(有关更多信息,请参阅本文(,因此在Java中通常无法获得任何东西的大小。但是,您可以使用HttpSessionAttributeListener(链接为JavaEE8及以下(跟踪会话中的内容。这将使您能够了解属性的数量,并在一定程度上了解正在使用的内存量。类似于:

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
@WebListener
public class MySessionAttributeListener implements HttpSessionAttributeListener {

@Override
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println( "the attribute "" + event.getName() + "" with the value "" + event.getValue() + "" has been added" );
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
System.our.println( "the attribute "" + event.getName() + "" with the value "" + event.getValue() + "" has been removed" );     
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
System.out.println( "the attribute "" + event.getName() + "" with the value "" + event.getValue() + "" has been replaced" );
}
}

最新更新