Practically Groovy: Go Server-Side Up, with Groovy
By Andrew Glover2005-05-05
Groovlets and GSPs
The prerequisites for working with Groovlets and GSPs are quite simple: You need a servlet container, and the latest and greatest version of Groovy. The beauty of these frameworks is that they map all URLs of a chosen pattern to a specific servlet via a web.xml file. Thus, the first step to setting up a Groovlets and GSP implementation is to define a Web application context and update its associated web.xml file. The file will include the specific servlet class definitions and their corresponding URL pattern.
I'll be using Apache Jakarta Tomcat, and I've created a context called groove. The directory layout is shown in Listing 3:
Listing 3. Directory listing of the groove context
./groove:In the WEB-INF directory, I need to have a web.xml file with at least the elements shown in Listing 4:
drwxrwxrwx+ 3 aglover users 0 Jan 19 12:14 WEB-INF
./WEB-INF:
-rwxrwxrwx+ 1 aglover users 906 Jan 16 14:37 web.xml
drwxrwxrwx+ 2 aglover users 0 Jan 19 17:12 lib
./WEB-INF/lib:
-rwxrwxrwx+ 1 aglover users 832173 Jan 16 14:28 groovy-1.0-beta-9.jar
-rwxrwxrwx+ 1 aglover users 26337 Jan 16 14:29 asm-1.5.2.jar
Listing 4. A fully configured web.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>The definitions in the above web.xml file state that any request ending with .groovy (for example, http://localhost:8080/groove/hello.groovy) will be sent to the class groovy.servlet.GroovyServlet, while any request ending with .gsp will go to the class groovy.servlet.TemplateServlet.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>GroovyServlet</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>GroovyTemplate</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GroovyServlet</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GroovyTemplate</servlet-name>
<url-pattern>*.gsp</url-pattern>
</servlet-mapping>
</web-app>
My next step is to place two jars in the lib directory: the groovy distribution jar (in my case, groovy-1.0-beta-9.jar) and the corresponding asm jar (asm-1.5.2.jar for groovy beta-9).
Yep, it's that easy -- I'm ready to go.
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
