Monday, June 02, 2008

Elegant Parsing Of Servlet Parameters

I love SOA. I use servlets to send back JSON data and I honestly believe it's the most elegant approach to asynchronous data over the web. Sure, AJAX and XML is great but JSON has some real flexibility when it comes to small size and larger payloads. I frequently use Java with a tomcat server to handle the data coming from the database. I get to use Hibernate and it makes life very easy.

Today's post is about parsing servlet parameters. If you use SOA enough you'll find youre coding new servlet parameters all the time and your head begins to swim with all the work involved in reading and validating parameters. This is where this tasty nugget comes into play. I developed this to read the parameters and place them into a hashmap. The nice part about this is that you read the parameter set one time and the hashmap handles all sorts of things for you.

First its easy to find stuff and check for missing parameters. You simply need to see if the key exists to know if you have a parameter. Next, there is no code overhead here in maintaining a list of the parameters you are checking and assigning them to variables, fields, etc. You simply reference the hash and convert when necessary. So you get cleaner code by having less of it. Try using this the next time you're coding a servlet and you'll find it makes your SOA go much faster.

Please remember to make a post here if you use anything. I like to see how people are using my code.

No comments: