Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Practically Groovy: Go Server-Side Up, with Groovy

By Andrew Glover
2005-05-05


What About Those GSPs?

So far, I've focused on writing Groovlets. As you'll see, however, Groovy's GSP pages easily complement the Groovlets framework, much like JSPs complement the Servlet API.

On the surface, GSPs look just like JSPs, but actually, they couldn't be more different because the GSP framework is really a template engine. If you're unfamiliar with template engines, you might want to quickly review last month's article.

While GSPs and JSPs are fundamentally different technologies, they are similar in that GSPs are excellent candidates for embodying the view of a Web application. As you might recall from last month, a view-facilitating technology lets you separate the concerns of your application's business logic and its corresponding view. If you take a quick look back at the diagnostic Groovlet in Listing 6, you might see where it could stand to be improved with GSP code.

Yep, that Groovlet is kind of ugly, isn't it? The problem is that it mixes application logic and a load of printlns to output the HTML. Fortunately, I can resolve the situation by creating a simple GSP to complement the Groovlet.

An example GSP
Creating GSPs is as easy as creating Groovlets. The key to GSP development is realizing that a GSP is essentially a template and, therefore, may be best served by limited logic. I'll create a simple GSP in Listing 7 to get us started:

Listing 7. A simple GSP

<html>

<head><title>index.gsp</title></head>
<body>
<b><% println "hello gsp" %></b>
<p>
<% wrd = "Groovy"
for (i in wrd){
%>
<h1> <%=i%> <br/>

<%} %>
</p>
</body>
</html>
Looking at the above GSP should seriously remind you of standard Groovy template development. The syntax is JSP-ish in its use of <%s, but, like the Groovlet framework, it lets you access common servlet objects, such as ServletRequest, ServletResponse, ServletContext, as well as HttpSession objects.

Tutorial Pages:
» On-the-fly Server-Side Programming with Groovlets and GSPs
» Defining Functions in Scripts
» Groovlets and GSPs
» The Groovlet, Please
» A Diagnostic Groovlet
» What About Those GSPs?
» Refactor me this ...
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources