Java Reference
In-Depth Information
The groovlet can set response headers, here setting the output to XML. Input parameters
populate a map of strings called params , which can be accessed in the usual way. The
URL requires two-digit days and two-digit months, so a zero is prepended when neces-
sary. After retrieving the games for that date the output is generated using the implicit
MarkupBuilder . There's no need to instantiate a MarkupBuilder in this case, be-
cause groovlets already contain one, called html .
The groovlet is called from a regular web page, using a URL of the form http://.../groovy-
baseball/GroovyService.groovy?month=10&day=28&year=2007 . The XML data is writ-
ten to the output stream, which can then be processed by JavaScript.
Lessons learned (Groovy Baseball)
1. POGOs have private attributes and public methods by default. Public getters and
setters are auto-generated for each attribute.
2. POGOs include a map-based constructor that can be used to set any or all of the
attributes in any combination.
3. Closures and methods in Groovy return their last evaluated expressions automatic-
ally.
4. The XmlSlurper class makes parsing XML simple and returns the root of the
resulting DOM tree. Values can be extracted by walking the tree.
5. The MarkupBuilder class produces XML.
6. The groovy.sql.Sql class is a simple façade for dealing with relational data-
bases.
7. Groovlets are simple Groovy scripts that respond to HTTP requests.
8. All Groovy exceptions are unchecked.
The rest of the system is just HTML and JavaScript, so it's beyond the scope of a Groovy
discussion. The complete source code for the application is contained in the GitHub repos-
itory for the topic.
Search WWH ::




Custom Search