Why does setting properties in a session sometimes fail in a java servlet?

I am developing a fairly simple web application using JSP and java servlet running on Tomcat I have been able to set properties in the session from the servlet to pass information to the JSP and then present it to the user I've done this with several different objects of different classes and it works well Suddenly, when I set a specific type of object (containing configuration information), the attribute did not appear in the JSP at all The other properties I set still exist, but the configuration object is completely lost I printed the list of attribute names, and the names I used didn't even exist (although other names of other attributes I set exist)

What could cause this? My configuration class is no different or strange I really appreciate any idea of what might lead to this behavior I googled and searched and couldn't find anything

ETA: if important, the attribute name is "configuration" I can't find anything about reserved words or anything... I set this property in the servlet the same way as others such as "user" Then I redirect to a JSP that tries to get the user and configuration So everything goes on at the same time The user is good, and the configuration does not even appear in the attribute name list

Eta2: the following are the recurring exceptions in the log:

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)

Solution

There are many possibilities for possible problems

>There may be two different sessions. > There may be some code to delete the configuration object from the session. > someone else?

This is a method that can be tested to determine what happened

You can implement two separate listener interfaces to listen for specific session events: javax servlet. http. HttpSessionListener and javax servlet. http. HttpSessionAttributeListener

I will use a class to implement these two interfaces, which will record what happens during each event and which session the event comes from

You should be able to easily add listeners to the web XML file so that they can actually be set as listeners for the session through Tomcat

edit

This is one that can be put into the web as a session listener XML class Balusc and I recommend that you try this method to debug problems If you see anything interesting in the way you set the configuration property, please just humor us and tell us?

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();
    }
}
}

Add the above class to the code, and then add the following to the web. XML between the filter map and the servlet map In the XML file:

<listener>
    <listener-class><your.package.name>SessionListener</listener-class>
</listener>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>