|
||||||||||
|
||||||||||
GroovyUsing Groovy
Language GuideGroovy FeaturesModulesExamplesUseful LinksIDE SupportSupportCommunityDevelopersTools we use
Feeds
|
You can write normal Java servlets in Groovy (i.e. Groovlets). 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.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.counter == null) { session.counter = 1 } html = new MarkupBuilder(out) html.html { head { title("Groovy Servlet") } body { p("Hello, ${request.remoteHost}: ${session.counter}! ${new Date()}") } } session.counter = session.counter + 1 Setting up groovyletsPut 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 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:
<Context path="/groovy" docBase="c:/groovy-servlet"/> Then access it with http://localhost:8080/groovy/hello.groovy |
|||||||||
|