Gretty Light-Weight Web Services Framework – Part Two

Thought to push the boundary a little with Gretty to see how i could have my browser provide more than a single data value to the server, but am handicapped by a severe shortage of javadocs or documentation.

Now let’s have two data values from the browser: name and game. The address in my browser looks like this :

http://localhost:8080/Mike?game=polo

To see more detail here, then click to grow :

Gretty Server Test
Gretty Server Test of Several Data Values

You can see these changes to the template file as follows :

<html>
 <head>
  <title>Hello!</title>
 </head>
 <body>
<h1>index.gpptl code</h1>
  <p>${message}</p>
  <p>I/P Address is ${addr}</p>
  <p>Host name is ${hname}</p>
  <p>Canonical name is ${cname}</p>
  <p>Here is message 1: ${message1}</p>
  <p>Here is message 2: ${message2}</p>
 </body>
</html>

and we’ve added a further message 2 to show all the data values coming from the browser.

//-----------
import org.mbte.gretty.httpserver.* 

@GrabResolver(name='gretty',
  root='http://groovypp.artifactoryonline.com/groovypp/libs-releases-local')
@Grab('org.mbte.groovypp:gretty:0.4.279')  

def addr = InetAddress.localHost.hostAddress
def hname = InetAddress.localHost.hostName
def cname =InetAddress.localHost.canonicalHostName

GrettyServer server = []
server.groovy =
[
    localAddress: new InetSocketAddress("localhost", 8080),
    defaultHandler:
    {
        response.redirect "/"
    },
    "/:name":
   {
        get
        {
		def namelist = request.getParameters()
   		response.html = template("index2.gpptl", [message: "Hello ${request.parameters['name']}", message1: "${request.parameters['game']}", message2:(namelist)?:'none', addr:"${addr}",hname:"${hname}",cname:"${cname}"    ])
        } // end of get
    } // end of
] 

server.start()

Have added a further bit of logic to get the list of all data parameters in the request.

namelist = request.getParameters()

and message two now shows us the map of data values. You can see the implied ‘name’ from the browser URL :

[game:polo, name:Mike]

Reading References: Interview with Alex Tkachman, creator of groovy++ and used as foundation of gretty web server. Can greety scale ? CAN GRETTY SCALE ? Read This !

Another IBM DeveloperWorks article has more examples of gretty in action.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s