简体   繁体   中英

Calling Groovy classes from Java EE + adding to jsp

Background

Hello, I'm new to Java EE. I'm following this tutorial 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.

Context

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

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

Additional Information / Thoughts

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

Expected output

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

Actual Output

I'm using ! That's

Here's the code:

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>Test 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>

JavaServlet.java

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

<?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>JavaServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JavaServlet</servlet-name>
        <url-pattern>/javacallinggroovy</url-pattern>
    </servlet-mapping>

</web-app>

main.groovy

class main {

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

Please add these in your pom.xml

<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM