Jun 14 2008
Groovlets and GSP, setting up
Groovelts and Groovy Server Pages are unbelievably simple to set-up and use, quick and dirty web-api’s and proof of concept can be implemented in no time!
Using groovy as a serverlet (also known as Groovlet) is a really cool thing. Grails is a must if you want to develop a complex web-app, but what about the small quickies that can be done in minutes? groovlets are surely the answer.
In just one-liner groovlet I could solve a problem at work that has been outstanding for weeks.
Here is how to set it up :
- Download and unzip groovy binary package and jetty servlet engine. in my case the two folders are ~/Development/jetty-6.1.11 and ~/Development/groovy-1.5.6. Needless to say, you need Java (Standard Edition JDK) preferably the latest release (mine is 1.6).
- Create your groovlet webapp folder structure
mkdir -p ~/Development/jetty-6.1.11/webapps/glet/WEB-INF/lib/
- Copy groovy run-time jar file to your servlet lib folder
cp ~/Development/groovy-1.5.6/embeddable/groovy-all-1.5.6.jar \ ~/Development/jetty-6.1.11/webapps/glet/WEB-INF/lib/
- Create ~/Development/jetty-6.1.11/webapps/glet/WEB-INF/web.xml file with the following content
<web-app version="2.4" 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 web-app_2_4.xsd" > <!-- Setup Groovlets --> <servlet> <servlet-name>glet</servlet-name> <servlet-class>groovy.servlet.GroovyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>glet</servlet-name> <url-pattern>*.glet</url-pattern> </servlet-mapping> <!-- Setup Groovy Server Pages (GSP) --> <servlet> <servlet-name>GroovyTemplate</servlet-name> <servlet-class>groovy.servlet.TemplateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GroovyTemplate</servlet-name> <url-pattern>*.gsp</url-pattern> </servlet-mapping> <!-- The Welcome File List --> <welcome-file-list> <welcome-file>index.glet</welcome-file> </welcome-file-list> </web-app>
- The Groovlet : Create ~/Development/jetty-6.1.11/webapps/glet/index.glet with the following content
def mylist = ["Hello", "World!"] html.html { head { title("My nice index page") } body { table { tr { td { span("This is a test page ...") br() i("In case you want italic") } } tr { td { mylist.each { html.span(it) html.br() } } } } span("Hello there") h1("Hala") h2{ i("nice meeting you") } b("cake") (0..3).each { p (it) } println "hala 3ammi" h1 "XML encoding with Groovy" p "this format can be used as an alternative markup to XML" /* an element with attributes and text content */ a href:'http://groovy.codehaus.org', "Groovy" p "some text" p ( ["this is some", b ("mixed"), "text."]) pre (["a", "b"]) { (0..3).each {println "danni"} } p "other text" } }
The above html style is called HTML Builder, and it gives a different paradigm for servlet development.
Another dummy example .. :if (!session) session = request.getSession(true); if (!session.counter) session.counter = 1 html.html { // html is implicitly bound to new MarkupBuilder(out) head { title("Groovy Servlet") } body { h1("Hello, ${request.remoteHost}:") p("${session.counter}! ${new Date()}") } } session.counter = session.counter.toInteger() + 1
- The Groovy Server Page Create ~/Development/jetty-6.1.11/webapps/glet/mypage.gsp with the following content
<html> <head><title>mypage.gsp</title></head> <body> <b><% println "mypage gsp" %></b> <p> <% wrd = "Groovy" for (i in wrd){ %> <h1> <%=i%> <br/> <%} %> </p> </body> </html>
Just for the record, this is not exactly the same GSP that comes with grails. The GSP module that comes with grails offers more features.
- Start jetty and browse to http://localhost:8080/glet
You can also do it in other ways, like using print “<html> … “ instead of the html DSL.
Here is a oneliner example groovlet :
println "<h1> Hello ${params["name"]} to the world of groovlets</h1>"
put this line in say this file :jetty-6.1.11/webapps/glet/hello.glet
then call it from here : http://localhost:8080/glet/hello.glet?name=kefah
I thought glet is a nice shortname for groovlet.
The nice thing about groovlets is that you can edit them and create new ones and they will work on the fly. no need to restart jetty or recompile code.
Enjoy.
Hi.. i am not able to run my groovlets
first i wil expain what i have done..
i downloaded groovy & place it into c:\groovy
downloded jetty & place it into c:\getty
now i created folder structure as follows
c:\getty\webapps\glet\WEB-INF\lib
copied all lib of groovy folder into that lib folder
& created web.xml file in that WEB-INF directory..
now i started server(jetty)…
but i am not able get index page..which i created
i think context wll be created somewhere..i dont know where it will be created…can u plz help me?
Hello Syaam,
You have copied the wrong groovy lib files. Please remove all the lib (.jar) files you previously copied to glet\WEB-INF\lib ;
Instead, you just need to copy this lib (.jar) file from groovy c:\groovy\embeddable\groovy-all-1.5.6.jar to jetty :
c:\getty\webapps\glet\WEB-INF\lib\groovy-all-1.5.6.jar
Let me know should that resolve the problem.
- Kefah.
Thank you for ur suggestion..still i am not able to get that..
i have one doubt..i created simply one folder glet& placing web-inf folder(lib+web.xml)into that..i think server must know this glet directory as some root for this groovy files..so is there is need of contexts or something else?
i created 2 files in glet directory test.html & index.glet
when i am running http://www.localhost:8080/glet/test.html(its Running)
http://www.localhost:8080/glet/index.glet(its not running)
can u give ur suggestions please?
Thanks in advance
Hi,
i have one more doubt..as per my knowledge, groovy file names are ending with .groovy extension..we are giving .glet as file extension..is it able to parse?
Hello Syaam,
glet directory must be created here : c:\getty\webapps
As you mentioned, under glet there should be :
index.glet
myotherfiles.glet
WEB-INF
under WEB-INF, there is web.xml file and the lib folder which would have the jar file i told you bout.
Just doing that, then starting jetty should be enough.
the extension .glet is what I specificed in web.xml its arbitrary, you can specify any thing else.
Cheers,
- Kefah.
Thank you for ur information…glet created in webapps only..i think directory structre what i have created is fine..if there is any problem in web.xml file..in that i am placing servlet name as glet(which is created in webapps) is it right?
can u xplain what is exactly servlet name…in that context..
Thanks
syaam
I think,servlet name is also arbitary thing..i understood how it maps also..
but i didnt understand what the problem is going on?
can u give me ur suggestions please?
Thank you,
syaam
Syaam,
I went all the way and set up Java/Jetty/Groovy on windows xp sp2. Following the exact steps above (taking care of the path differences between Linux and Windows of course), and it worked out of the box.
Please review the steps above carefully (which I have updated to be more specific).
Beyond that I’m afraid I wont be of any help.
Regards,
- Kefah.