My first Groovy CGI implementation

Posted : May 5, 2004 at 1:39 pm [America/Los_Angeles]

While writing the actual code was a whole lot of fun, configuring Apache to actually respond to Groovy as CGI programs involved a few hacks (details below). I also wrote a Perl and Python version (along with the Groovy implementation) just to enable folks to get a very high-level comparitive overview. Needless to say, this exercise is purely to get a feel of how “scripty” Groovy is. I have no desire to build Groovy/CGI applications (as the speed difference in the demo links above will show)

So here we go:

Groovy Implementation:

#!/usr/bin/env groovy

stuff = "Hello, Groovy"

# Spitting out standard HTTP Header
println "Content-type: text/htmlnn"

# Using here-docs to generated HTML content
html = <<<OUTPUT
<html>
<head>
<title>Hello, Groovy</title>
</head>
<body background="/images/blue-dash.gif">
<h3>${stuff}</h3>
</body>
</html>
OUTPUT

# Spitting out standard Hello, Groovy
println html

Python Implementation:

#!/usr/bin/env python

stuff = "Hello, Python"

# Spitting out standard HTTP Header
print "Content-type: text/htmlnn"

# Using here-docs to generated HTML content
# Thanks to James for clarifying how to use
# here-docs in Python..:-)
html = """
<html>
<head>
<title>Hello, Python</title>
</head>
<body background="/images/blue-dash.gif">
<h3>%s</h3>
</body>
</html>""" % stuff

# Spitting out standard Hello, Python
print html

Perl Implementation:

#!/usr/bin/env perl

$stuff = "Hello, Perl";

# Spitting out standard HTTP Header
print "Content-type: text/htmlnn" ;

# Using here-docs to generated HTML content
$html = <<OUTPUT;
<html>
<head>
<title>Hello, Perl</title>
</head>
<body background="/images/blue-dash.gif">
<h3>$stuff</h3>
</body>
</html>
OUTPUT

# Spitting out standard Hello, Perl
print $html;

In order to make Groovy CGI Applications work, I had to make two changes:
1. Update the envvars file (in APACHE_HOME/bin) as follows:

# Set JAVA_HOME
JAVA_HOME=”/usr/local/j2sdk1.4.2″
export JAVA_HOME
# Set GROOVY HOME
GROOVY_HOME=”/usr/local/groovy-1.0-beta-4″
export GROOVY_HOME
# PATH
PATH=$PATH:${GROOVY_HOME}/bin:${JAVA_HOME}/bin
export PATH

2. Update the httpd.conf as follows:

PassEnv GROOVY_HOME
PassEnv JAVA_HOME
PassEnv PATH

Enjoy.

Update:

Decided to remove the links to my CGI as it was ‘killing’ my machine

- Anand

Viewed: 7116 times

13 Comments »

groovy will perform like a dog as CGI — it will require a full JVM load to run each time =(

Posted by: Brian McCallister at May 5, 2004 @ 4:05 pm

Tell me about it..;-) - Thx

Posted by: Anand Sharma at May 5, 2004 @ 5:25 pm

Can Apache’s mod_groovy be far away? :)

Vinny

Posted by: Vinny Carpenter at May 5, 2004 @ 8:17 pm

Uh, what’s the &tt;/h3> for?

Posted by: Tim Bray at May 5, 2004 @ 10:10 pm

Good catch. Pleased to have someone “actually” peruse the code :-) Actually, I wanted to type &-l-t-; and ended by typing &-t-t-; Should be fixed now.

- Thx Anand
p.s. The hyphens above are just for illustration. Still looking for a good tool/way to post my HTML code in my blog entries.

Posted by: Anand Sharma at May 5, 2004 @ 10:52 pm

mod_groovy sounds like a plan to me. Or, I guess we could just all go write Groovlets. Thx for the post. - Anand

Posted by: Anand Sharma at May 5, 2004 @ 10:55 pm

It sounds like a cool stuff~
Waiting for your next release.
I have write an post about your implmenention in my blog~
but do not find the trackback url~

see here: http://icecloud.51.net/blog/archives/000081.html
I’ve write it in chinese :P

Posted by: Li Mo at May 6, 2004 @ 8:06 am

What are ‘here-docs’? Perhaps I can tell you how to do them in Python if I know what they are!

Posted by: Simon Brunning at May 6, 2004 @ 9:14 am

here-doc is the ability to define a variable like this:

s = <<<EOS
 This string
 spans three "lines"
 and contains two newlines.
EOS

I know Python supports “”" to print multiple lines. I just don’t know if it supports it in the following context:

blah = """
This is a multiple-line
varible """

Posted by: Anand Sharma at May 6, 2004 @ 7:52 pm

Thanks for showing interest, Li. Well, I am sure my next implementation will not be another Groovy/CGI. I am trying to get to the point where I can learn the language well enough to start doing web application development on Tomcat using servlets (not groovlets) written in Groovy.

I am not sure I am impressed with Groovlets that much. Maybe my impressions will change as I get to know more.

Posted by: Anand Sharma at May 6, 2004 @ 7:56 pm

You can indeed do:

blah = “”"
This is a multiple-line
varible “”"

Posted by: Simon Brunning at May 7, 2004 @ 1:31 am

Thanks Simon. I updated the sample code in the post (for Python). One new thing learnt on a “Friday”. Not a bad start at all..:-)

Posted by: Anand Sharma at May 7, 2004 @ 9:28 am

I know this is an old post, but give http://www.easygsp.com a look

Posted by: David at November 25, 2009 @ 8:20 am

Leave a Comment