Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:

Practically Groovy: Go Server-Side Up, with Groovy

By Andrew Glover
2005-05-05


The Groovlet, Please

Writing Groovlets is indubitably simple, since Groovy poses few requirements in terms of class hierarchy extensions. With Groovlets, there is no need to extend from javax.servlet.http.HttpServlet, javax.servlet.GenericServlet, or some slick GroovyServlet class. In fact, creating a Groovlet is as simple as creating a Groovy script. You don't even have to create a class. In Listing 5, I've written a simple Groovlet that does two things: It prints some HTML, then it provides some information about the container it's running in.

Listing 5. Getting started with Groovlets

println """

<html><head>
<title>Groovlets 101</title>
</head>
<body>
<p>
Welcome to Groovlets 101. As you can see
this Groovlet is fairly simple.
</p>
<p>
This course is being run on the following servlet container: </br>
${application.getServerInfo()}
</p>
</body>
</html>
"""
If you viewed this Groovlet in your browser, it would look something like what you see in Figure 1.

Figure 1. Output from the simple Groovlet


Looking closely at the Groovlet in Listing 5 should take you back to the time when you first started writing Groovy scripts. First, there is no main method or class definition, just some simple code. What's more, the Groovlet framework implicitly provides instance variables, such as ServletRequest, ServletResponse, ServletContext, and HttpSession. See how I was able to reference the instance of ServletContext via the application variable? If I wanted to grab the instance of HttpSession, I'd use the session variable name. Similarly, I can use request and response for ServletRequest and ServletResponse, respectively.

Tutorial Pages:
» On-the-fly Server-Side Programming with Groovlets and GSPs
» Defining Functions in Scripts
» Groovlets and GSPs
» The Groovlet, Please
» A Diagnostic Groovlet
» What About Those GSPs?
» Refactor me this ...
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT