Why does setting an attribute in a session from a Java servlet sometimes fail?

I am working on a fairly simple web application using JSP and a Java servlet running on tomcat. I was able to set the attributes in the session from the servlet to pass information to the JSP, which are then passed to the user. I did this with several different objects of different classes, and it worked fine. Suddenly, when I set a specific type of object (containing configuration information), the attribute does not appear at all in the JSP. The remaining attributes that I set still exist, but the configuration object is completely missing. I printed out a list of attribute names, and the name I used does not even exist (although there are other names for the other attributes that I set).

What could be the reason for this? There is nothing unusual or strange in my configuration class. I would really appreciate any ideas on what things might cause this behavior. I searched Google and searched and found nothing.

ETA: If the value has an attribute value, this is “configuration”. I could not find anything that would be a reserved word or something else ... I set this attribute in the servlet in the same function as several others, such as "user". Then I redirect the JSP, which is trying to get both the user and the configuration. So everything happens simultaneously. the user is fine, while the configuration does not even appear in the list of attribute names.

ETA2: Here is the exception that is logged:

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) 
+6
java jsp tomcat servlets
source share
2 answers

There are many possibilities in what may go wrong.

  • There may be two different sessions.
  • There may be some code that removes your configuration object from the session.
  • Others?

Here you can check what exactly is happening.

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

I would implement these two interfaces with a class that will register what happens during each event, and with what session the event occurs.

You should be able to easily add your listeners to your web.xml so that they are truly set as listeners in your tomcat sessions.

EDIT

Here is a class that you can place in your web.xml as a listener for your sessions. Both BalusC and I recommended that you try this approach to debug the problem. Please just humor us and let us know if you see anything interesting in how your “configuration” attribute is set?

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

Please add the above class to your code, and then add the following to your web.xml file between your filter mappings and servlet mappings:

 <listener> <listener-class><your.package.name>SessionListener</listener-class> </listener> 
+4
source share

According to the comments on the question:

The session is completely absent, and I can get other attributes that were set in the servlet at the same time as the session in JSP. This is only one specific attribute that will not appear. I found many things in the session that were not there, but not why one attribute will not work when others do it.

Then something deleted or canceled the attribute.

 session.removeAttribute("name"); 

or

 session.setAttribute("name", null); 

or even in jsp

 <c:set var="name" value="${null}" scope="session" /> 

Or it could be the one who set null instead of a full object.

To attach one and the other better, I would let the attribute implement the HttpSessionBindingListener and drop the stack to valueUnbound() .

 public class Foo implements HttpSessionBindingListener { @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println("Value bound"); // Logger? } @Override public void valueUnbound(HttpSessionBindingEvent event) { System.err.println("Value unbound"); // Logger? Thread.dumpStack(); } // ... } 
+6
source share

All Articles