Pages

Thursday, December 27, 2012

Tomcat 7: Custom Valve

I was asked today by a colleague for some help on creating a valve for use with Apache Tomcat 7. I had never done one before, so I thought I would give it a quick try. +Craig McClanahan  had done a great job on creating a simple mechanism for creating a Valve with a lot of the heavy lifting done by ValveBase.

Creating your own valve is really simple using NetBeans 7.2.1 along with Apache Maven.

  1. Create a Maven Java Application.
  2. Add the following dependency:
  3. Create your Java class and extend it from ValveBase.
  4. Implement invoke(Request request, Response response)
  5. Build your library (.jar) file
  6. Install the library in the ${tomcat.home}/lib directory.
  7. Configure server.xml to use your new Valve. For example:
  8. Start the server to see your new valve in action
Here is my simple example.

ProcessingValve.java

Here is the output...
INFO: Server startup in 1442 ms
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080

As you can see it was very easy to implement. Here is the code for my example: valve-example.zip

The source can also be found on Bitbucket: valve-example

2 comments :