Calling Groovy classes from Java EE + adding to jsp

admin

Administrator
Staff member
<strong>Background</strong>

Hello, I'm new to Java EE. I'm following <a href="https://milkedeek.wordpress.com/2012/08/20/groovy-and-tomcat-pt1/" rel="nofollow noreferrer">this tutorial</a> that outlines how to call a groovy class from a java project. Reason for doing this is a lot of pre-existing code I have built is written in groovy, and is fairly extensible for what I would like to do.

<strong>Context</strong>

From a Java and Groovy perspective, the code works. However when firing up the tomcat server and opening
Code:
index.jsp
, I am not able to hit the code.

When looking at the
Code:
index.jsp
file, I see error:
Code:
cannot resolve variable
for
Code:
language
,
Code:
sentiment
, and
Code:
message
.

<strong>Additional Information / Thoughts</strong>

Could this be something to do with my dependencies?
I used
Code:
maven
to import
Code:
ant:ant-antlr:1.6.5
,
Code:
asm:asm-all:3.3.1
,
Code:
jstl:jstl:1.2
,
Code:
org.codehaus.groovy:groovy-all:2.2.1
. Are they the wrong ones? Before adding dependencies,
Code:
Java.sun.com/jsp/jstl/core
was not able to be found (obviously)

<strong>Expected output</strong>

Open a web browser and the page displays "I'm using java! that's okay.... I was called from groovy, Exciting"

<strong>Actual Output</strong>

I'm using ! That's

<strong>Here's the code:</strong>

index.jsp:

Code:
&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;

&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Test Sandbox&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;I'm using &lt;c:out value="${language}" /&gt;! That's &lt;c:out value="${sentiment}" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;c:out value="${message}" /&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

JavaServlet.java

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

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");
        req.getSession().setAttribute("message", main.message());
    }
}

web.xml

Code:
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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"&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;JavaServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;JavaServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;JavaServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/javacallinggroovy&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

&lt;/web-app&gt;

main.groovy

Code:
class main {

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