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.

5 thoughts on “Groovy and Tomcat, Pt1 – Calling Groovy from a Java Servlet

  1. Hi Eek,
    i need some help with one of my requirements which is kind of similar to this post.
    I have a login page (userDetail.jsp) and i wish to copy the text entered by the user in the textbox and copy that to some variable and send it over HTTP post or any other way to a groovy page(userDetails.gsp) with the same fields(i.e FirstName,LastName etc) and paste those values copied from .jsp page to grrovy page fields.Hope i was able to make it clear.
    Thanks In Advance
    -Lucky

    • Hi Ajit,

      Thanks for the comment. As for your question, the easiest way is the following:

      In your .jsp your form links to a .gsp in its action attribute:

      form action=”userDetails.gsp”

      The Form links to your gsp where you print the information:

      println request.getParameter(“firstName”)

      There you have it. But, this does not process the data in any way. The best way would be:

      1. userDetail.jsp sends form data to a servlet
      2. servlet processes data
      3. servlet sends redirect to userDetails.gsp
      4. userDetails.gsp shows the fields

      That’s a better way.

      Hope this helps,
      Eek.

      P.S.: I included code examples, but wordpress seems to dispose of them, if you need an example: I’ll release an all-including project later this week.

  2. Thanks Eek for the reply,
    yeah that will help,but if you can send me some details for how to retrieve that data in groovy page,because i’m new to grrovy-Grails,i have no idea of how to do that, any code example will be extremely helpful.Thanks again for replying.

Leave a Reply