You can write normal Java servlets in Groovy (i.e. Groovlets).
There is also a
which automatically compile your .groovy source files, turn them into bytecode, load the Class and cache it until you change the source file.
Here's a simple example to show you the kind of thing you can do from a Groovlet.
Notice the use of implicit variables to access the session, output & request.
import java.util.Date if (session == null) { session = request.getSession(true); } if (session.counter == null) { session.counter = 1 } println """ <html> <head> <title>Groovy Servlet</title> </head> <body> Hello, ${request.remoteHost}: ${session.counter}! ${new Date()} </body> </html> """ session.counter = session.counter + 1
Or, do the same thing using MarkupBuilder:
import java.util.Date import groovy.xml.MarkupBuilder if (session == null) { session = request.getSession(true); } if (session.counter == null) { session.counter = 1 } html.html { // html is implicitly bound to new MarkupBuilder(out) head { title("Groovy Servlet") } body { p("Hello, ${request.remoteHost}: ${session.counter}! ${new Date()}") } } session.counter = session.counter + 1
Implicit variables
The following variables are ready for use in Groovlets:
| variable name | bound to | note |
|---|---|---|
| request | ServletRequest | - |
| response | ServletResponse | - |
| context | ServletContext | unlike Struts |
| application | ServletContext | unlike Struts |
| session | getSession(false) | can be null! see A |
| out | response.getWriter() | see B |
| sout | response.getOutputStream() | see B |
| html | new MarkupBuilder(out) | see B |
A The session variable is only set, if there was already a session object. See the 'if (session == null)' checks in the examples above.
B These variables cannot be re-assigned inside a Groovlet. They are bound on first access, allowing to e.g. calling methods on the 'response' object before using 'out'.
Setting up groovylets
Put the following in your web.xml:
<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>
Then all the groovy jar files into WEB-INF/lib. You should only need to put the groovy.jar, the antlr.jar and the asm.jar. Or copy the groovy-all-xyz.jar into WEB-INF/lib - this almost all jar contains the antlr and asm jars.
Now put the .groovy files in, say, the root directory (i.e. where you would put your html files). The groovy servlet takes care of compiling the .groovy files.
So for example using tomcat you could edit tomcat/conf/server.xml like so:
<Context path="/groovy" docBase="c:/groovy-servlet"/>
Then access it with http://localhost:8080/groovy/hello.groovy
Comments (5)
Sep 01, 2005
carlemueller says:
I would like the addition of another implicit variable: binding, which maps to S...I would like the addition of another implicit variable:
binding, which maps to ServletBinding.getVariables()
This will enable fancy stuff like:
String id = 56775;
String name = "Tonisha"
// a hibernate hql with named parameters...
hqlquery = new hqlquery("SELECT * FROM SomeTable WHERE Name=:name AND Id=:id")
// get list of parameters
namedPs = hqlquery.getNamedParameters()
// iterate through and assign from binding...
for (int i=0; i < namedPs.size(); i++)
{
hqlquery.setParameter(namedPs[i],binding[namedPs[i]]);
}
So, as variables are declared in the script and placed into the binding's variable map, they can be looked up from the same Groovlet via map syntax for stuff like above
Dec 30, 2005
Dave Webb says:
When printling from closures, you need to use out.println \1,2\.each \ will...When printling from closures, you need to use out.println
[1,2].each { println it } will print to stdout
[1,2].each { out.println it } will print to the (HTTP) response stream
Jun 01, 2007
Jürgen Hermann says:
"binding" is named "this"."binding" is named "this".
Jun 26, 2007
倪森 says:
I would like the addition URL parameters as implicit variable: for example: Groo...I would like the addition URL parameters as implicit variable:
for example: Groovlets?showComments=true&showCommentArea=true
in java ,
in groovlet
Jun 26, 2007
倪森 says:
binding named this (y) 。 this is better than in。 this=Attributes=Parametersbinding named this
。 this is better than in。 this=Attributes=Parameters