Comparing Water to JSP for building web sites
When building a web site, how does Water
compare to other technologies?
The first and second articles in this series
showed how to build a simple program with a few
lines of code.
The Water code in the first articles was very simple and compact.
How does it actually compare to the code required to do the same
thing in one of the leading web development
platforms, JSP and Java? This article demonstrates how a 7-line
program written in Water has the same functionality as a 65-line
program written in JSP and Java. The comparison is summarized
in the following table:
|
# Files |
# Lines |
# Characters |
# Languages |
| JSP, Java, and Custom Tag Libraries |
4 |
65 |
1276 |
4 |
| Water |
1 |
7 |
130 |
1 |
Water is a new XML-Native object-oriented
programming language that allows you to program in XML.
It is an open language
designed to simplify the creation of new
Web services and programs. Water adheres to a "Learn Once,
Use Everywhere" philosophy where a single language
unifies data, logic, and presentation. An interpreter
is freely available from www.waterlanguage.org. The Water Runtime
is deployed on a standard Servlet engine. Clear Methods, www.clearmethods.com,
sells a commercially supported XML-Native platform based on
the Water language.
The Task: Define a function and call it from HTML
A common task in web development is to define some HTML
that can be inserted from multiple pages. Each page might need
the HTML in different variations. For example,
a standard footer shown on multiple pages might have a "You are here"
indicator for the current page.
The task is to create a new HTML tag with a single parameter, message,
and use the tag from an HTML page to insert the chunk of HTML.
This description does not sound very complicated, but the
implementation is about 10 times smaller in Water than other
technologies such as JSP or ASP. Before we view the
code, I will describe several ways in which Water simplifies
the development of Web services and programs.
Why should you program in XML?
XML is a standard syntax for
representing hierarchical data, but there is not an XML
standard for representing arbitrary data and logic.
The Water language is a general-purpose
object-oriented programming language designed for
XML. Since you can program in Water, it extends XML to
let you implement logic, data, and presentation. Water is XML-Native
because it is a superset
of XML, and it is backward compatible with XML version 1.0.
For the task in this article, a parameterized chunk of HTML is
simply a method, and inserting it in multiple pages is just a method call.
The Water language has two important features
that significantly reduce the complexity
of web development:
1. Water shares the same syntax as HTML and XML.
Web development is unnecessarily complex due to the number of
different technologies and languages required to build
and deploying Web software.
For example programming with JSP, which is only a part of the J2EE
platform, requires learning at least four different languages
or technologies.
- HTML is the base markup language predominately used
to create thin-client user interfaces.
- JSP tags such as page directives and
expressions bridge the HTML and Java languages.
- Java is used to implement application flow and business logic
- Tag Library Descriptors describe custom
JSP tags that call Java functions.
In addition, web programs often require the use of the JavaScript language and
the Cascading Style Sheet language.
Each language has a different syntax, a different
method for creating objects, and different processing rules.
Each of these differences makes it challenging to implement
even simple programs. Tracking the location of a simple
syntax error or debugging an n-tier system is extremely difficult.
For example, a JSP file contains HTML text.
The HTML embeds JSP tags, and the JSP tags embed Java code.
The Java code can output strings, but not JSP tags.
Manipulating HTML and XML
typically requires a lot of parsing and string processing.
For example, the HTML tags within a JSP file are simple strings
and therefore the majority of JSP operations are
string-oriented, not object-oriented. On the other hand,
Water treats every HTML tag as either a method call or an
object constructor. A developer can use the
Water debugger to step through nested HTML tags and show
HTML tags in the object inspector. Extending the
object system to HTML is one of the key simplifications
that Water brings to web development.
The Water language is pure XML; Water's syntax and
object system blend seamlessly with HTML and XML.
Other programming
languages have an object model that makes it difficult
to interface to XML. For example, there are at least
four officially supported, but incompatible XML interfaces
from Sun, as well as many other XML API's from other vendors.
The syntax is familiar to most web developers because
the Hypertext Markup Language is the most widely used
language in the world. Unlike JSP and ASP technologies,
which require training in many languages, Water has only
a single, simple language to learn.
2. Water supports a flexible architecture
In a typical N-tier architecture, each tier requires
different languages. For example, the client
uses HTML and JavaScript, but the server uses JSP and Java.
JSP is not available on the client.
However, Water is a single language that may be used
in every tier. This is
possible because the Water Runtime can run
in every tier, including a browser, web server,
application server, email server, and database.
Water interfaces to existing systems
such as J2EE, relational databases, legacy systems,
file system, email, and web pages.
Using a common language across every
tier dramatically simplifies the construction
of new Web services and programs.
From the developer's perspective,
Water provides a consistent method to work with presentation, logic,
and data in every tier. A function call is the same in every
tier. Local function calls look like remote function calls
over HTTP. A function call from a web form to a Water server
has the same structure as a function call between two servers.
A mail message can also act as a function call.
The business logic runs where it makes the
most sense for the business. In other platforms,
the technology constrains
the deployment architecture because each technology
only works within a single tier.
Water removes these restrictions and opens up many
new possibilities to build and deploy Web
services and programs quickly and easily.
Water Implementation
Below is the Water code to implement the task of
defining a function and calling it from HTML.
<defmethod new_tag message="message not available">
message
</defmethod>
<HTML>
<new_tag message="This is a tag with an attribute"/>
</HTML>
Creating a custom tag in JSP is the equivalent to
defining a new object or method in Water. The defmethod
defines a new method, new_tag, with
a single parameter message. The default value of
the parameter is "message not available". The method
returns the value of the message argument
passed into the method. The method returns the
value of message because a Water method returns
the value of the last expression.
JSP Implementation
The JSP implementation requires four files to implement this
task:
- Java file
- Tag Library Descriptor
- Web.XML configuration file
- JSP file
A Java file
implements the functionality of the custom tag. The Tag Library
Descriptor file describes the structure of the custom tag and
names the Java class file.
The web.xml configuration file stores the location of
the Tag Library Descriptor file.
The JSP file represents a single
web page that embeds the custom tag in HTML.
Java File: new_tag.java
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class new_tag extends javax.servlet.jsp.tagext.TagSupport
{
private String message = "message not available";
public int doStartTag() throws JspException
{
try
{
pageContext.getOut().print(message);
}
catch (Exception e)
{
throw new JspTagException(e.getMessage());
}
return SKIP_BODY;
}
public void setMessage(String text)
{
message = text;
}
}
Tag Library Descriptor: example-taglib.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>new_tag</shortname>
<info>Info about the new tag</info>
<tag>
<name>new_tag</name>
<tagclass>new_tag</tagclass>
<attribute>
<name>message</name>
<required>false</required>
</attribute>
</tag>
</taglib>
Configuration file: web.xml
<taglib>
<taglib-uri>
http://java.apache.org/tomcat/examples-taglib
</taglib-uri>
<taglib-location>
/WEB-INF/jsp/example-taglib.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>
http://www.jsptest.com/taglib
</taglib-uri>
<taglib-location>
/WEB-INF/taglib.tld
</taglib-location>
</taglib>
JSP file: page_file.jsp
<%@ taglib uri="http://www.jsptest.com/taglib" prefix="ctl" %>
<HTML>
<ctl:new_tag message="Creating a new function with JSP"></ctl:new_tag>
</HTML>
Summary
In JSP, implementing a simple function and calling it from an
HTML page requires four files in four different languages.
Not only is the Water implementation more than 10 times smaller,
it is much easier to write, debug, deploy, and maintain.
Water's "Learn Once, Use Everywhere" philosophy and XML-Native
design allows dramatic simplifications in Web development.