我面临一个问题,即已部署的 Web 应用程序(没有源代码或任何引用)似乎与某些会话参数有问题。我不知道如何对该应用程序进行逆向工程,所以我觉得我可以展示此事件的唯一方法是使用 log4j 或 tomcat 自己的 JULI 工具来获取会话参数列表。我读过有关 Wrox 的 Professional Apache Tomcat 6 的文章,坦率地说,它在这方面并没有太大帮助......
关于使用 log4j 或 JULI 的方向有什么提示吗?
提前谢谢了。
您可以添加一个过滤器以将会话属性列表打印到 Log4J,如下所示:
public class SessionAttributesLoggingFilter implements Filter {
private Logger log = Logger.getLogger(this.getClass());
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = httpRequest.getSession();
Enumeration<String> sessionAttributeNames = session.getAttributeNames();
log.info("Session attributes before request ----------------------->");
while (sessionAttributeNames.hasMoreElements()) {
String attr = sessionAttributeNames.nextElement();
log.info(" "+attr + " = " + session.getAttribute(attr));
}
chain.doFilter(request, response);
sessionAttributeNames = session.getAttributeNames();
log.info("Session attributes after request ----------------------->");
while (sessionAttributeNames.hasMoreElements()) {
String attr = sessionAttributeNames.nextElement();
log.info(" "+attr + " = " + session.getAttribute(attr));
}
} else {
chain.doFilter(request, response);
}
}
public void init(FilterConfig arg0) throws ServletException {
log.info("Logging session attributes ...");
}
public void destroy() {}
}
然后将映射添加到您的 web.xml 并重新启动您的应用程序:
<filter>
<filter-name>sessionLogging</filter-name>
<filter-class>PACKAGENAME.SessionAttributesLoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionLogging</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>