blob: 538ddc67ef417546f83848d0e524325923a3080a [file] [log] [blame]
1 Groovlets: Writing Servlets in Groovy
You can write normal Java servlets in Groovy.
There is also a {link:GroovyServlet|http://groovy.codehaus.org/apidocs/groovy/servlet/GroovyServlet.html} 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.
{code:groovlet}
import java.util.Date
if (session.counter == null) {
session.counter = 1
}
out.println(<<<EOS
<html>
<head>
<title>Groovy Servlet</title>
</head>
<body>
Hello, ${request.remoteHost}: ${session.counter}! ${new Date()}
<br>src
</body>
</html>
EOS)
session.counter = session.counter + 1
{code}
1.1 Setting up groovylets
Put the following in your web.xml:
{code: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>
{code}
Then all the groovy jar files into WEB-INF/lib. (You should only need to put
the groovy jar and the asm jar).
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:
{code:xml}
<Context path="/groovy" docBase="c:/groovy-servlet"/>
{code}
Then access it with http://localhost:8080/groovy/hello.groovy