Groovy and Tomcat, Pt5 – Bringing it together

For the final part in this miniseries I’ll create a small web application which brings together everything discussed so far. We’ll be using a Java Servlet, a Groovy Servlet and Groovyscripts to bring you the ultimate mix of all things Groovy that aren’t GRails.

What does it do?
It counts the visits you make and adds them to either the javacounter or the groovycounter. There is also a small groovyscript resetting the counter.

Web.xml

<servlet>
 <servlet-name>JavaCount</servlet-name>
 <servlet-class>net.mymilkedeek.groovytomcat.project.JavaCounter</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>JavaCount</servlet-name>
 <url-pattern>/javacount</url-pattern>
 </servlet-mapping>
 <servlet>
 <servlet-name>GroovyCount</servlet-name>
 <servlet-class>net.mymilkedeek.groovytomcat.project.GroovyCounter</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>GroovyCount</servlet-name>
 <url-pattern>/groovycount</url-pattern>
 </servlet-mapping>

counter.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
 <title></title>
</head>
<body>
 <p>Calls made:</p>
 <table>
 <tr>
 <td>Java</td>
 <td><c:out value="${javacounter}" /></td>
 </tr>
 <tr>
 <td>Groovy</td>
 <td><c:out value="${groovycounter}" /></td>
 </tr>
 </table>
</body>
</html>

reset.groovy

request.session.setAttribute("javacounter", 0)
request.session.setAttribute("groovycounter", 0)

response.sendRedirect("counter.jsp")

Java Servlet

public class JavaCounter extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 Object countObject = req.getSession().getAttribute("javacounter");

 int count = 0;

 if ( countObject != null ) {
 count = (Integer) countObject;
 }

 req.getSession().setAttribute("javacounter", count+1);

 resp.sendRedirect("counter.jsp");

Groovy Servlet

class GroovyCounter extends HttpServlet {

 @Override
 protected void doGet (HttpServletRequest req, HttpServletResponse resp) {
 def countObject = req.session.getAttribute("groovycounter")

 def count = 0

 if ( countObject != null ) {
 count = (Integer) countObject
 }

 req.session.setAttribute("groovycounter", count+1)

 resp.sendRedirect("counter.jsp")
 }
}

And that’s the end of this series. The code can be checked out at: https://bitbucket.org/mymilkedeek/tutorials/src

Groovy and Tomcat, Pt4 – Groovy Servlet Pages

Groovy Server Pages (GSP)

Setting up GSP is as easy as setting up scripts.

Add following to the project:

– groovy.jar
– groovy-json.jar
– groovy-servlet.jar
– groovy-templates.jar
– groovy-xml.jar
– antlr.jar
– asm.jar

web.xml


<servlet>
 <servlet-name>GroovyPages</servlet-name>
 <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>GroovyPages</servlet-name>
 <url-pattern>*.gsp</url-pattern>
 </servlet-mapping>

And then just create your .gsp file:


<html>
<head><title>Groovy Pages</title></head>
<body>
<p><% println "hello world" %></p>
</body>
</html>

Navigating to the gsp url gives me the following:

Still easy as 1-2-3.

The next part will be up later tonight as it’s under construction. It will feature a project that ties all the different groovy parts together into one project.

Until then, take care,
Eek.

Groovy and Tomcat, Pt3 – Groovy Scripts

In this part I’m going to show you how to use Groovy Scripts on a Tomcat. It’s very easy to do!

First add these libraries to your project:

– groovy.jar
– groovy-json.jar
– groovy-servlet.jar
– groovy-xml.jar
– antlr.jar
– asm.jar

You might need to add more to the project depending on the functionality of your scripts, but this is the minimum I needed to not encounter any exceptions.

Next, in your web.xmladd the following servlet;


<servlet>
 <servlet-name>GroovyScript</servlet-name>
 <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
 </servlet>

<servlet-mapping>
 <servlet-name>GroovyScript</servlet-name>
 <url-pattern>*.groovy</url-pattern>
 </servlet-mapping>

Groovy will then compile your scripts and run them as requested. For a quick test, put this in your webapp root folder and have its name end with .groovy, I named mine script.groovy, but you should name it something more meaningful.


def ip = request.getRemoteAddr()

println """
<html>
<head>
<title>Groovy Scripts</title>
</head>
<body>
<p>Hello ${ip}</p>
</body>
</html>
"""

Navigate to the url of the script and you’ll get a warm welcome.

Wasn’t that easy?

Stay tuned,
Eek.

Groovy and Tomcat, Pt2 – Groovy extends HttpServlet

Continuing where we left off…

The jars you’ll need for this part are the same as the ones for part 1:

– groovy.jar
– antlr.jar
– asm.jar

GroovyHttpServlet


package net.mymilkedeek.tomcat

import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class GroovyHttpServlet extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
 req.session.setAttribute("language", "groovy")
 req.session.setAttribute"sentiment", "awesome")
 req.session.setAttribute("message", JavaGroovy.message())
 resp.sendRedirect("index.jsp")
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
 super.doPost(req, resp)
 }
}

And add it to the web.xml

</pre>
<servlet>
 <servlet-name>GroovyHttpServlet</servlet-name>
 <servlet-class>net.mymilkedeek.tomcat.GroovyHttpServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>GroovyHttpServlet</servlet-name>
 <url-pattern>/groovyhttpservlet</url-pattern>
 </servlet-mapping>
<pre>

Now, navigate to the Servlet url and watch Groovy take care of everything:

So, in short, what we did was replace a Java HttpServlet with a Groovy HttpServlet.

For my next blog post, I’ll show you how to set up a Groovy Script in a webapplication.

Stay tuned,
Eek.

Groovy and Tomcat, Pt1 – Calling Groovy from a Java Servlet

For this first post, I’ll keep it very simple: a Java servlet calls Groovy code to display a message to the screen. Start by setting up a regular Java servlet application. After your simple web application is set up, read the code snippets below.


index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Eek's Groovy Sandbox</title>
</head>
<body>
<p>I'm using <c:out value="${language}" />! That's <c:out value="${sentiment}" /></p>
<p><c:out value="${message}" /></p>
</body>
</html>

In this .jsp code, we’ll print three attributes. Two handled by the Java servlet and one handed to us by our Groovy utility class.

The Java servlet


package net.mymilkedeek.tomcat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * The Java Servlet
 *
 * @author My Milked Eek
 */
public class JavaServlet extends HttpServlet {
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 req.getSession().setAttribute("language", "java");
 req.getSession().setAttribute("sentiment", "ok...");
 resp.sendRedirect("index.jsp"); }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">

<servlet>
 <servlet-name>JavaServlet</servlet-name>
 <servlet-class>net.mymilkedeek.tomcat.JavaServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>JavaServlet</servlet-name>
 <url-pattern>/javacallinggroovy</url-pattern>
</servlet-mapping>

</web-app>

That’s a simple webapplication. Now add the following dependencies to your project:

– groovy.jar
– antlr.jar
– asm.jar

Now add a Groovy Class, I named it JavaGroovy.

Groovy Class

package net.mymilkedeek.tomcat

class JavaGroovy {

static def message() {
 "I was called from Groovy. Exciting, isn't it?"
 }
}

And add following line to your Java Servlet:

...
req.getSession().setAttribute("message", JavaGroovy.message());
...

Now, navigate to the url of the Java Servlet and watch the magic happen:

So, in short, what we did was make a call to a Java Servlet. This servlet then gets a message from a Groovy class. And then we added that message to the session.

This kind of setup with Groovy is particularly useful with an existing Java Servlet: You only need to add Groovy jars and you can start hacking away.

For my next post, I’ll show you how to get Groovy extending HttpServlet.

Stay tuned,
Eek.