Java Reference
In-Depth Information
<servlet>
<servlet-name>Groovy</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Groovy</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>
The GroovyServlet class is part of the standard Groovy library. Here it's mapped to
the URL pattern *.groovy , which means that any URL that ends in that pattern will be
directed to this servlet. For example, the URL http://localhost/.../hello.groovy would match
a script named hello.groovy in the root of the web application. Keep in mind that this
is literally the source file, not the compiled class.
Groovlets
Groovlets are deployed as source code, not compiled.
When invoked, the GroovyServlet class finds the script whose name ends the URL,
pre-instantiates a series of variables, creates an instance of the GroovyScriptEngine
class, and executes the script. The actual script code can be placed in any accessible direct-
ory from the web application root, or in any subdirectory of /WEB-INF/groovy.
The key to the simplicity of groovlets is this already-configured infrastructure. With this in
place a developer has a lot less work to do.
10.2.1. A “Hello, World!” groovlet
Because every technology needs a “Hello, World!” application, here's a groovlet to greet
the user. Assume that the GroovyServlet has already been configured, and add a file
called hello.groovy in the root of a web application. In a standard Maven structure
that would be src/main/webapp/hello.groovy. The contents of the groovlet are
name = params.name ?: 'World'
println "Hello, $name!"
Search WWH ::




Custom Search