P
Paul Carey
Hi
I'm seeing behaviour that I really don't understand and was hoping
someone could explain it to me. I've written a class that extends
HttpServlet and implements HttpSessionAttributeListener. The class
contains a single instance variable, attributes, which is an ArrayList
that's initialised in its declaration. What I don't understand is that
attributes appears to be a completely different object depending on
whether it's used in a HttpServlet method it overrides, or a
HttpSessionAttributeListener method it implements.
The logs look me this:
doGet: session 1a302615671063879637250 ArrayList attributes: size is
0, hashCode is 1
attributeAdded: (frustrated, true) added to session
1a302615671063879637250
ArrayList attributes: size is 3, hashCode is 1588958425
name: org.apache.struts.action.LOCALE value: en
name: APP_DATE_FORMAT value: M/d/yy
name: frustrated value: true
doGet: session 1a302615671063879637250 ArrayList attributes: size is
0, hashCode is 1
The code should be fairly clear. Any explanations at what I'm seeing
and why would be much appreciated.
Thanks in advance
Paul
public class SessionAttributeDetails extends HttpServlet implements
HttpSessionAttributeListener
{
private ArrayList attributes = new ArrayList();
public void attributeAdded( HttpSessionBindingEvent sbe )
{
attributes.add( sbe );
System.out.println( "attributeAdded: (" + sbe.getName() + ", "
+ sbe.getValue() + ") added to session " + sbe.getSession().getId() );
System.out.println( "\tArrayList attributes: size is " +
attributes.size() + ", hashCode is " + attributes.hashCode() );
for( Iterator itr = attributes.iterator(); itr.hasNext(); ) {
sbe = (HttpSessionBindingEvent) itr.next();
System.out.println( "\tname: " + sbe.getName() + " value:
" + sbe.getValue() );
}
}
public void attributeRemoved( HttpSessionBindingEvent sbe )
{
attributes.remove( sbe );
System.out.println( "attributeRemoved: (" + sbe.getName() + ",
" + sbe.getValue() + ") removed from session " +
sbe.getSession().getId() );
System.out.println( "\tArrayList attributes: size is " +
attributes.size() + ", hashCode is " + attributes.hashCode() );
}
public void attributeReplaced( HttpSessionBindingEvent sbe )
{
}
public void doGet( HttpServletRequest request, HttpServletResponse
response ) throws IOException, ServletException
{
System.out.println( "doGet: session " +
request.getSession().getId() + " ArrayList attributes: size is " +
attributes.size() +
", hashCode is " + attributes.hashCode() );
HttpSession session = request.getSession();
session.setAttribute( "frustrated", "true" );
...
}
}
By modifying the code slightly, setting attributes to null and
initialising it in the init method like so:
private ArrayList attributes = null;
public void init() throws ServletException
{
attributes = new ArrayList();
attributes.add( "definately not null" );
System.out.println( "init: attributes is " + attributes + "
hashCode is " + attributes.hashCode() );
}
the logs will then contain the very helpful
init: attributes is [definately not null] hashCode is -1048832968
attributeAdded: attributes is null
I'm seeing behaviour that I really don't understand and was hoping
someone could explain it to me. I've written a class that extends
HttpServlet and implements HttpSessionAttributeListener. The class
contains a single instance variable, attributes, which is an ArrayList
that's initialised in its declaration. What I don't understand is that
attributes appears to be a completely different object depending on
whether it's used in a HttpServlet method it overrides, or a
HttpSessionAttributeListener method it implements.
The logs look me this:
doGet: session 1a302615671063879637250 ArrayList attributes: size is
0, hashCode is 1
attributeAdded: (frustrated, true) added to session
1a302615671063879637250
ArrayList attributes: size is 3, hashCode is 1588958425
name: org.apache.struts.action.LOCALE value: en
name: APP_DATE_FORMAT value: M/d/yy
name: frustrated value: true
doGet: session 1a302615671063879637250 ArrayList attributes: size is
0, hashCode is 1
The code should be fairly clear. Any explanations at what I'm seeing
and why would be much appreciated.
Thanks in advance
Paul
public class SessionAttributeDetails extends HttpServlet implements
HttpSessionAttributeListener
{
private ArrayList attributes = new ArrayList();
public void attributeAdded( HttpSessionBindingEvent sbe )
{
attributes.add( sbe );
System.out.println( "attributeAdded: (" + sbe.getName() + ", "
+ sbe.getValue() + ") added to session " + sbe.getSession().getId() );
System.out.println( "\tArrayList attributes: size is " +
attributes.size() + ", hashCode is " + attributes.hashCode() );
for( Iterator itr = attributes.iterator(); itr.hasNext(); ) {
sbe = (HttpSessionBindingEvent) itr.next();
System.out.println( "\tname: " + sbe.getName() + " value:
" + sbe.getValue() );
}
}
public void attributeRemoved( HttpSessionBindingEvent sbe )
{
attributes.remove( sbe );
System.out.println( "attributeRemoved: (" + sbe.getName() + ",
" + sbe.getValue() + ") removed from session " +
sbe.getSession().getId() );
System.out.println( "\tArrayList attributes: size is " +
attributes.size() + ", hashCode is " + attributes.hashCode() );
}
public void attributeReplaced( HttpSessionBindingEvent sbe )
{
}
public void doGet( HttpServletRequest request, HttpServletResponse
response ) throws IOException, ServletException
{
System.out.println( "doGet: session " +
request.getSession().getId() + " ArrayList attributes: size is " +
attributes.size() +
", hashCode is " + attributes.hashCode() );
HttpSession session = request.getSession();
session.setAttribute( "frustrated", "true" );
...
}
}
By modifying the code slightly, setting attributes to null and
initialising it in the init method like so:
private ArrayList attributes = null;
public void init() throws ServletException
{
attributes = new ArrayList();
attributes.add( "definately not null" );
System.out.println( "init: attributes is " + attributes + "
hashCode is " + attributes.hashCode() );
}
the logs will then contain the very helpful
init: attributes is [definately not null] hashCode is -1048832968
attributeAdded: attributes is null