Simple groovy pages with Jetty

Filed under: Development, Java — lars @ 07:50:09 am

Today I set up Jetty so that I could play around with some very quick and simple gsp (groovy server pages) development using my favorite light-weight servlet container, Jetty.  No building or packaging - just editing .gsp files in a directory - PHP style.

Here's how I did it:

  1. I downloaded and unzipped jetty 8.1.3.
     
  2. I edited $JETTY_HOME/etc/jetty.xml to change the default port to one that was free on my PC:
    <Set name="port"><Property name="jetty.port" default="8081"/></Set>
     
  3. In the contexts directory, I modified the out-of-the-box test.xml and changed the contextPath from "/" to "/test" (because I wanted my groovy pages to use the root context "/"):
    <Set name="contextPath">/test</Set>
     
  4. Then I copied text.xml to a file called root.xml.  I set the contextPath to "/" and the "war" path to /webapps/defaultroot:
    <?xml version="1.0"  encoding="ISO-8859-1"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="contextPath">/</Set>
      <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/defaultroot</Set>
      <Set name="extractWAR">true</Set>
      <Set name="copyWebDir">false</Set>
      <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
    </Configure>
     
  5. Then in the folder $JETTY_HOME/webapps. I created a directory called "defaultroot" and put a simple "Hellow World" test index.jsp in there.
     
  6. Now start jetty from the command-line using "java -jar start.jar".  If you access http://localhost:8081/ in your browser, you should see the contenst of your test "Hello World".
     
  7. If we're good to this point, we're now ready to add groovy support.  I downloaded Groovy 1.8 (binary), and from the zip file extracted the file embeddable/groovy-all-1.8.6.jar from the zip, and copied it to $JETTY_HOME/lib/ext.
     
  8. Then, I edited $JETTY_HOME/etc/webdefault.xml and added the following servlet mapping after the JSP Servlet mapping:
    <!-- ==================================================================== -->
    <!-- Groovy/GSP Servlet                                                   -->
    <!-- ==================================================================== -->
    <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>

    <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>
     
    This means that by default, every web app deployed on your Jetty instance will be able to process groovy pages with URLs of .groovy and .gsp - including the "defaultroot" app we created above.
     
  9. Rename your test "index.jsp" in "defaultroot" to "index.gsp" and restart Jetty.  Now, when you hit http://localhost:8081 in your browser, if your groovy page has been processed successfully you should still see your test Hellow World!
     
  10. Now you're good to go.  Here's a good introduction to Groovy Server Pages to get you started:
    http://www.javabeat.net/articles/print.php?article_id=249

Comments

No Comments for this post yet...

    Leave a comment

    Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>


    Options:
    (Line breaks become <br />)
    (Set cookies for name, email & url)




    powered by  b2evolution