Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext and ServletContextAttribute listener query

 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 listener classes : One that implements ServletContextListener and other that implements ServletContextAttributeLIstener.

I have put println statements in each of the implemented methods of the corresponding listener.

I am using tomcat as container.
In my deployment descriptor I have the following entry for context init parameters:
<context-param>
<param-name> context name </param-name>
<param-value> context value </param-value>
</context-param>

When I start tomcat I get the below output in the console.

Jun 9, 2007 10:48:19 AM org.apache.coyote.http11.Http11BaseProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Jun 9, 2007 10:48:19 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 906 ms
Jun 9, 2007 10:48:19 AM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Jun 9, 2007 10:48:19 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.5.23
Jun 9, 2007 10:48:19 AM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
context initializes
attribute added:
context initializes
attribute added:
Jun 9, 2007 10:48:20 AM org.apache.coyote.http11.Http11BaseProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jun 9, 2007 10:48:20 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 9, 2007 10:48:20 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/47 config=null
Jun 9, 2007 10:48:20 AM org.apache.catalina.storeconfig.StoreLoader load
INFO: Find registry server-registry.xml at classpath resource
Jun 9, 2007 10:48:20 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1108 ms

As can be seen by the bold lines, the ServletContextEvent and ServletContextAttributeEvent are fired.

I can understand the initialization part, but what about the attribute one, i havent added any attribute, yet I get the corresponding event is fired.
Also from the output it seems that the initialization takes place twice.

I wonder if the container initialized the ServletContext object with some value and also adds some attribute to it. Please let me know if this is the case.

Also, is there a way by which the attribute that was added to the context object can be obtained in the corresponding listener class. The event.getSource() returns object of ServletContext I guess.

Regards,
Joshua
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wonder if the container initialized the ServletContext object with some value and also adds some attribute to it. Please let me know if this is the case.


You have entry for the <context-param>... in the DD, so this is what container adds it to the servletContext as I see.


Also, is there a way by which the attribute that was added to the context object can be obtained in the corresponding listener class. The event.getSource() returns object of ServletContext I guess.



Yeah, your listener class has access to ServletContext. You can obtain
attributes there.
Not sure about, why HttpSessionAttributeListener fired. Put some print
statements there to see whether the methods are called.


Thanks,
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure about, why HttpSessionAttributeListener fired. Put some print
statements there to see whether the methods are called.


Yes , the methods are called.

The following output :
context initializes
attribute added:
context initializes
attribute added:


in the console is the result of the print statements in the corresponding listener classes.

You have entry for the <context-param>... in the DD, so this is what container adds it to the servletContext as I see.

I have added only one initialization parameter but as can be seen in the console output the initialization takes place twice.

Regards,
Joshua
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic