debuglistener.java
来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 95 行
JAVA
95 行
package debugging;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DebugListener implements ServletContextListener,
HttpSessionListener,
ServletContextAttributeListener,
HttpSessionAttributeListener {
private ServletContext servletContext=null;
private void logContext(String message) {
if (servletContext != null) {
StringBuffer messageBuffer = new StringBuffer();
messageBuffer.append("<invocation><sender>");
messageBuffer.append(servletContext.getServerInfo());
messageBuffer.append("</sender><message>" + message + "</message><receiver>");
String contextName=servletContext.getServletContextName();
if (contextName==null) contextName=servletContext.toString();
messageBuffer.append("CONTEXT: " + contextName);
messageBuffer.append("</receiver></invocation>");
servletContext.log(messageBuffer.toString());
}
}
private void logSession(String message, HttpSessionEvent event) {
if (servletContext != null) {
StringBuffer messageBuffer = new StringBuffer();
messageBuffer.append("<invocation><sender>");
messageBuffer.append(servletContext.getServerInfo());
messageBuffer.append("</sender><message>" + message + "</message><receiver>");
messageBuffer.append("SESSION: " + event.getSession().getId());
messageBuffer.append("</receiver></invocation>");
servletContext.log(messageBuffer.toString());
}
}
public void contextInitialized(ServletContextEvent event) {
servletContext=event.getServletContext();
logContext("init()");
}
public void contextDestroyed(ServletContextEvent event) {
logContext("destroy");
servletContext=null;
}
public void sessionCreated(HttpSessionEvent event) {
logSession("create", event);
}
public void sessionDestroyed(HttpSessionEvent event) {
logSession("destroy", event);
}
public void attributeAdded(ServletContextAttributeEvent event) {
logContext("setAttribute(" +
event.getName() + "," +
event.getValue().toString() + ")");
}
public void attributeReplaced(ServletContextAttributeEvent event) {
logContext("setAttribute(" +
event.getName() + "," +
event.getValue().toString() + ")");
}
public void attributeRemoved(ServletContextAttributeEvent event) {
logContext("removeAttribute(" + event.getName() + ")");
}
public void attributeAdded(HttpSessionBindingEvent event) {
logSession("setAttribute(" +
event.getName() + "," +
event.getValue().toString() + ")",
event);
}
public void attributeReplaced(HttpSessionBindingEvent event) {
logSession("setAttribute(" +
event.getName() + "," +
event.getValue().toString() + ")",
event);
}
public void attributeRemoved(HttpSessionBindingEvent event) {
logSession("removeAttribute(" + event.getName() + ")",
event);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?