1) Introduction
In this article, we will learn about how to achieve Web Development using Groovy. Groovy is a scripting language and it has components called Groovlets which sit on top of a Web Server for handling HTTP Requests and Responses similar to Java Servlets. This article will provide an overview about Groovlets in the first section and will present several code snippets that will help in simplifying the usage of Groovlets in the subsequent sections. Remember, this is not an introductory article on Groovy and first-time readers are strongly advised to have a look over the Introductory article on Groovy .
2) Groovlets
Before getting into Groovlets, let us look into the basics of a typical Java Web Application. Usually a Web Server will host one or more Web Applications and the Client's Requests are handled by Components called Java Servlets which usually sit on top of the Web Server. A Servlet continue to live within the Web Server's address space and its life-time will be controlled only by the Web Server. One of the major responsibilities of a Web Server is to match a client request to a particular Servlet in an Application. Typically, this is done in the Application's configuration file, usually web.xml file.
So a Java Servlet can be considered as an extension point which provides some useful services to a Client. The Web Server wraps the Client request and passes a Http Request object to the Servlet. The Servlet retrieves various inputs from the Client and executes some actions accordingly. A Servlet is written in pure Java code and one can see a mix of Java Business logic as well as Html UI Generation in a Servlet.
Now, let us see the Java Servlet's counterpart, Groovlets. Groovlets, or Groovy Pages that end with .groovy extension are processed by Groovy Engine which sits on top of a Web Server. Typically, a Groovy Engine is nothing but a Groovy Servlet. When a client requests for a Groovy page for the very first time, the Groovy Servlet will be invoked (we will configure this in the Application's descriptor file) and parses the page to convert into a Java class or a Java Servlet. The Java Servlet is then compiled and an instance of the class will be created by the Web Server. Groovlets are much suitable only for simpler or small Web Applications. Compared to Java Servlets, coding in Groovy will be much simpler.
In the next few sections, we will see how to develop Groovlets or Groovy pages.
|