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.

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

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.


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


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.


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.


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

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

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

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


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.