微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在Java servlet中设置会话中的属性有时会失败?

我正在使用在tomcat上运行的JSP和 Java servlet开发一个相当简单的Web应用程序.我已经能够从servlet中设置会话中的属性,以便将信息传递给JSP,然后将其呈现给用户.我已经用不同类的几个不同对象完成了这个并且它运行良好.突然间,当我设置一个特定类型的对象(包含配置信息)时,该属性根本没有出现在JSP中.我设置的其他属性仍然存在,但配置对象完全丢失.我打印了属性名称列表,我使用的名称甚至没有(尽管我设置的其他属性的其他名称都存在).

什么可能导致这个?我的配置类没什么不同或奇怪的.我真的很感激任何关于什么样的事情可能导致这种行为的想法.我用Google搜索搜索,找不到任何东西.

ETA:如果重要,则属性名称为“configuration”.我找不到任何关于保留字或任何内容的东西……我在servlet中设置这个属性功能与其他一些例如“user”相同.然后我重定向一个试图获得用户和配置的JSP.所以一切都在同一时间进行.用户很好,而配置甚至没有出现在属性名称列表中.

ETA2:以下是日志中不断发生的异常:

java.lang.Exception
    at pms.SessionListener.printstacktrace(UnkNown Source)
    at pms.SessionListener.attributeAdded(UnkNown Source)
    at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1498)
    at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1390)
    at org.apache.catalina.session.StandardSessionFacade.setAttribute(StandardSessionFacade.java:154)
    at PMS.getTaskInfo(UnkNown Source)
    at PMS.doGet(UnkNown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:282)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1687)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(UnkNown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnkNown Source)
    at java.lang.Thread.run(UnkNown Source)

解决方法

可能出现问题的可能性很多.

>可能会有两个不同的会话.
>可能会有一些代码从会话中删除配置对象.
>其他人?

这是一种可以测试以确定究竟发生了什么的方法.

您可以实现两个单独的侦听器接口来侦听特定的会话事件:javax.servlet.http.HttpSessionListenerjavax.servlet.http.HttpSessionAttributeListener

我将使用一个类来实现这两个接口,该类将记录每个事件期间发生的事情以及事件来自哪个会话.

您应该能够轻松地将侦听器添加到web.xml文件中,以便它们实际上可以通过tomcat设置为会话的侦听器.

编辑

这是一个可以作为会话监听器放入web.xml的类. BalusC和我都建议您尝试这种方法来调试问题.如果您在设置“配置”属性的方式中看到任何有趣的内容,请只是幽我们并告诉我们?

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionListener implements HttpSessionListener,HttpSessionAttributeListener,HttpSessionBindingListener,HttpSessionActivationListener {

public void valueBound(HttpSessionBindingEvent event) {
    System.out.println("valueBound: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printstacktrace();
}

public void valueUnbound(HttpSessionBindingEvent event) {
    System.out.println("valueUnbound: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printstacktrace();
}

public void attributeAdded(HttpSessionBindingEvent event) {
    System.out.println("attributeAdded: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printstacktrace();
}

public void attributeRemoved(HttpSessionBindingEvent event) {
    System.out.println("attributeRemoved: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printstacktrace();
}

public void attributeReplaced(HttpSessionBindingEvent event) {
    System.out.println("attributeReplaced: " + event.getName() + " : " + event.getValue());
    System.out.println("  session: " + event.getSession().getId());
    this.printstacktrace();
}

public void sessionCreated(HttpSessionEvent event) {
    System.out.println("sessionCreated: " + event.getSession().getId());
    this.printstacktrace();
}

public void sessionDestroyed(HttpSessionEvent event) {
    System.out.println("sessionDestroyed: " + event.getSession().getId());
    this.printstacktrace();
}

public void sessionDidActivate(HttpSessionEvent event) {
    System.out.println("sessionDidActivate: " + event.getSession().getId());
    this.printstacktrace();
}

@Override
public void sessionWillPassivate(HttpSessionEvent event) {
    System.out.println("sessionWillPassivate: " + event.getSession().getId());
    this.printstacktrace();
}

private void printstacktrace() {
    try {
        if (true) {
            throw new Exception();
        }
    } catch (Exception e) {
        e.printstacktrace();
    }
}
}

请将上面的类添加代码中,然后将以下内容添加到过滤器映射和servlet映射之间的web.xml文件中:

<listener>
    <listener-class><your.package.name>SessionListener</listener-class>
</listener>

原文地址:https://www.jb51.cc/java/121785.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐