TechHui

Hawaiʻi's Technology Community

This post is for those who've heard about Serlvets and Groovy, but just don't have the time to put them together. Being no guru, I've developed a number of JSPs and Servlets in my professional carrier. Every time it is the same thing.. remembering method signatures, xml descriptors, etc.. It gets even worse if you want a Servlet to print out HTML content. Let's just put it this way - Java is not designed for simple Web development. There is always the JSP approach, which is still pretty cumbersome, having to remember proper syntax to separate JSP code from HTML. At the end, every time I have to face Servlet development, I can't help but wonder if there is a better way. Now there is! I've heard about Groovy, but who has the time to actually learn a new language these days.. I had the same philosophy until faced with Servlets once again. This time, my Servlet had to produce a lot of dynamic HTML content. Without spending too much time on Java punctuation, I switched my attention to
Groovy. Then, wondering, if I can apply Groovy's simplicity to Servlets,
Groovlet emerged :) So what are the advantages of Groovlets vs Servlets. There are few. First of all, Groovlets are easier to setup. Include the following into your web.xml and its good enough for all your Groovlets in a project.

Then drop groovy-all.jar into your WEB-INF/lib directory. Secondly, printing dynamic HTML content is much simpler. Just do the following to print a simple Hello response:

Notice how easy it is to use dynamic content, just wrap your variables in ${..} and you are done. Now save your new Groovlet in web project root folder with .groovy extension. Assuming you are running on localhost, deployed your project as '/groovy' and named your Groovlet 'hello.groovy', use the following URL to access it:
http://localhost:8080/groovy/hello.groovy
Wow.. That was simple! Now most of the fun starts here. Basically you can use almost any Java feature available out there with Groovy syntax and Groovlet simplicity, plus use Groovy specific features to further minimize coding efforts. If you are new to Groovy, here are some more examples to wet your appetite: Declaring a variable
def myVar = "Some content here"
Print a list of cookies stored in request object
request.cookies.each() { println it.name }
Writing 'Hello World' to a file
new File(filename).write('Hello World\n')
Printing contents of a file
new File(path).eachLine { println it }
Listing a directory
new File("." ).eachFile{ file -> println file }
Connecting to a database
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/mydb", "user", "pswd", "com.mysql.jdbc.Driver")
Looping through rows
sql.eachRow("select * from attrs where type=${groovy}") { println "Groovy is ${it.name}" }
Groovy also provides simplified support for XML manipulation, quite handy for developing RSS, WebServices, etc.. Here is one way to parse an RSS feed
new XmlSlurper().parseText(rssFeed).channel.item.each{ println it.title } 
The good thing is that I can still re-use all my existing Java code. Servlets that used to take lines and lines of code get minimized to few paragraphs. Code becomes more legible and easier to manage. In short, Groovlet approach has significantly cut down my development time, not to mention, I am, once again, in love.. with Groovlets! :) Happy Grooving! hello.groovy
web.xml

Views: 1576

Comment

You need to be a member of TechHui to add comments!

Join TechHui

Comment by Daniel Leuck on April 21, 2009 at 6:35pm
I like the simplicity. I think my next servlet is going to be a Groovlet.

We really need literal strings in Java ("""...""").

Sponsors

web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service