Friday, December 20, 2013

Writing a simple tomcat valve and listener

Tomcat valves act as  preprocessors of each request for webapps. If we want to hook up our custom processing logic for requests, we can write a valve and register it globally for all webapps or locally for a webapp. 

To register it locally for a webapp, in the Context.xml of the webapp register it as;

<Context>
.....
 <Valve className="org.test.valve.MessageRecoveryTomcatValve"/>
.....
</Context>

To define the valve  globally, register it in the catalina-server.xml file

< Engine>
   <Host>
.........
      <Valve className="org.test.valve.MessageRecoveryTomcatValve"/>
.......
   </Host>
</Engine>

Tomcat listeners get notifications when there is any lifecycle state change occurs in a webapp.



This can be registered in the Context.xml of the webapp.

<Listener className="org.test.MyListener" context="GenericJavaBeanResource"/>