Search This Blog

Wednesday 3 June 2009

Running Groovlets in JDeveloper 11g

The following shows you how to setup a project in JDeveloper 11g to use Groovlets (Java servlets in Groovy). For more information on Groovlets see the following link.

http://groovy.codehaus.org/Groovlets

1. Download the groovy distribution 1.6.3 from this location and extract to your file system

http://dist.groovy.codehaus.org/distributions/groovy-binary-1.6.3.zip


2. In JDeveloper 11g select "File -> New -> General -> Applications -> Generic Application".
3. Name the Application "GroovyDemos" and click next.
4. Name the project "ServletDemo" and click next.
5. Click finish.
6. Double click on the project and select "Java EE Application".
7. Set the context root to "groovy" and click ok.
8. Add new JSP page called index.jsp.
9. Remove all code and add the following to the JSP page source code editor.

<%
response.sendRedirect("/groovy/sessiondemo.groovy");
%>

Note: JDeveloper 11g doesn't support running groovy scripts so we will get a JSP to forward to our actual groovy script which we setup in a later step

Now at this point we have a web project which we can now setup to run Groovlets with.

10. Navigate to the file system where you have the WEB-INF folder as shown below

$JDEV_HOME\mywork\GroovyDemos\ServletDemo\public_html\WEB-INF

11. Create a directory called "lib"
12. Place the file below which you can obtain from the groovy distribution in step1 into the lib folder created in step #11.

$GROOVY_HOME\embeddable\groovy-all-1.6.3.jar

13. Refresh the application navigator to show the JAR file we added
14. Add a groovy script file called "sessiondemo.groovy" to the public_html directory of your project.

$JDEV_HOME\mywork\GroovyDemos\ServletDemo\public_html


if (!session) {
session = request.getSession(true);
}

if (!session.counter) {
session.counter = 1
}

println """
<html>
<head>
<title>Groovy Servlet in JDEV 11g</title>
</head>
<body>
<h1> Groovy Servlet in JDEV 11g </h1>
Hello, ${request.remoteHost}: ${session.counter}! ${new Date()}
</body>
</html>
"""
session.counter = session.counter + 1

15. Edit web.xml and add the following entries for the "GroovyServlet".

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

16. Run index.jsp

The JSP page will then redirect the request to sessiondemo.groovy which will use GroovyServlet to actually compile your .groovy source file, turn them into bytecode, load the Class and cache it until you change the source file.

Your project would look as follows in the application navigator.