Evolution of Java Web technologies
The Enterprise Java language has always included Web technologies. These started out with servlets and have evolved from there.
Servlets were originally used to respond to HTTP requests. Writing a servlet was generally a pretty ugly affair. Take a look at an example in Listing 1.
Listing 1. Servlet generating HTML
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=ISO-8859-1\" />");
out.println("<title>All Users</title>");
out.println("</head>");
out.println("<body>");
out.println(" <table>");
out.println(" <tr>");
out.println(" <td>UserID</td>");
out.println(" <td>UserName</td>");
out.println(" <td>Name</td>");
out.println(" </tr>");
UserDao dao = new UserDao();
List users = dao.getAllUsers();
for (int i=0;i<users.size();i++){
User user = (User) users.get(i);
out.println(" <tr>");
out.println(" <td>"+user.getId()+"</td>");
out.println(" <td>"+user.getUserName()+"</td>");
out.println(" <td>"+user.getFirstName()+'
'+user.getLastName()+"</td>");
out.println(" </tr>");
}
out.println(" </table>");
out.println("</body>");
out.println("</html>");
}
|
Servlets involved lots of HTML (for example) being embedded in Java code. The only thing more difficult than developing servlets was maintaining them. Imagine that you wanted to change the table to have a border in the code in Listing 1. Doing so involved changing Java code and recompiling the servlet. Luckily, servlets were soon augmented with JavaServer Pages (JSP) technology.
JSP technology enhanced Java servlets. JSP components offered a lot of improvements over servlets, including allowing the native markup to be mixed with Java code. Listing 2 shows the same servlet shown in Listing 1 as a JSP component.
Listing 2. JSP 1.0 version
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="org.developerworks.*" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>All Users</title>
</head>
<body>
<table>
<tr>
<td>UserID</td>
<td>UserName</td>
<td>Name</td>
</tr>
<%
UserDao dao = new UserDao();
List users = dao.getAllUsers();
for (int i=0;i<users.size();i++){
User user = (User) users.get(i);
%>
<tr>
<td><%= user.getId() %></td>
<td><%= user.getUserName() %></td>
<td><%= user.getFirstName() %> <%= user.getLastName()
%></td>
</tr>
<%
}
%>
</c:forEach>
</table>
</body>
</html>
|
Obviously, what you see in Listing 2 is a big improvement over the servlet. JSP components were still compiled into a servlet, but this was done by the servlet container (or it could be done as part of a build). So, a JSP component could offer the same performance as a servlet. The PHP syntax is similar to Active Server Page (ASP) and PHP pages, but the compiling into a servlet gave JSP components a significant performance advantage over these other technologies.
There are still some significant issues to the JSP code shown in Listing 2. It uses a scriptlet, which is a small piece of Java code. Using scriptlets presented problems from both a design and a practical standpoint. The JSP components freely mixed business logic (retrieving the list of users) with presentation. The JSP Model 2 architecture evolved to solve this. A servlet could be used in conjunction with the JSP component. Take a look at Listing 3.
Listing 3. Model 2-style servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserDao dao = new UserDao();
List users = dao.getAllUsers();
request.setAttribute("users", users);
request.getRequestDispatcher("/user.jsp").forward(request, response);
}
|
The servlet could first handle the request and perform business logic. It could
then store the results in the HttpServletRequest object
and forward to a JSP component. This allowed the JSP component to be simplified, as seen in Listing 4.
Listing 4. Model 2-style JSP component
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="org.developerworks.*" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>All Users</title>
</head>
<body>
<table>
<tr>
<td>UserID</td>
<td>UserName</td>
<td>Name</td>
</tr>
<%
List users = (List) request.getAttribute("users");
for (int i=0;i<users.size();i++){
User user = (User) users.get(i);
%>
<tr>
<td><%= user.getId() %></td>
<td><%= user.getUserName() %></td>
<td><%= user.getFirstName() %> <%= user.getLastName()
%></td>
</tr>
<%
}
%>
</c:forEach>
</table>
</body>
</html>
|
That solves some of the architectural issue. However, there's still a practical problem. The mixing of Java code and HTML makes the JSP components difficult to work with for both Java developers who don't know HTML that well and for Web designers who don't know the Java language well.
The JavaServer Pages Standard Tag Library (JSTL)
Cleaning up JSP components to remove any Java scriptlets became a goal for JSP technology. The end result of this quest was the JSTL. The JSTL introduced HTML-style tags for accessing Java objects and performing many of the constructs of the Java programming language, such as iterating over collections, conditional logic, and formatting text. JSTL allows the JSP component to evolve, as shown in Listing 5.
Listing 5. JSP with JSTL
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>All Users</title>
</head>
<body>
<table>
<tr>
<td>UserID</td>
<td>UserName</td>
<td>Name</td>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td><c:out value="${user.id}"/></td>
<td><c:out value="${user.userName}"/></td>
<td><c:out value="${user.firstName}"/> <c:out
value="${user.lastName}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
|
The <c:forEach> tag allows iteration
over the list of users. The <c:out> tag
allows the Java object to be accessed and its data output.
The expressions used in the tags, such as ${users} and
${user.id}, were interpreted by the JSTL Expression
Language (EL). For example, the EL interprets the string
${users} and looks for an attribute called
users in the various objects accessible, such as the
pageContext, request,
session, and servlet
(application) context. JSP components evolved further to allow the EL to be accessed outside
of JSTL tags. This leads to the JSP components in Listing 6.
Listing 6. JSP using JSTL and EL
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>All Users</title>
</head>
<body>
<table>
<tr>
<td>UserID</td>
<td>UserName</td>
<td>Name</td>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.userName}</td>
<td>${user.firstName} ${user.lastName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
|
This is the modern form of JSTL and the EL first seen in the JSP 2.0 specification. When coupled with a Model 2 architecture (often implemented by various UI frameworks, such as Apache Struts and others), this combination of the JSTL and EL lets you build JSP components that are essentially Java free. This allows non-Java programmers to work with the JSP components and lets Java developers concentrate on implementing the business logic of their applications.
However, JSP technology isn't the only Web technology that's part of the Enterprise Java architecture. Shortly after the JSP 2.0 specification debuted, JSF technology also debuted. JSF was designed to be a component architecture. Objects on a Web page are viewed as components with a life cycle and with Java objects bound to them. Thus, in this article's JSP example, you can directly bind the Java object to the view component using JSF. The resulting JSP component is shown in Listing 7.
Listing 7. JSP using JSF
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http:.//java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>All Users</title>
</head>
<body>
<f:view>
<h:dataTable id="users" value="#{UserBean.users}" var="user">
<h:column>
<f:facet name="header">UserID</f:facet>
<h:outputText value="#{user.id}"/>
</h:column>
<h:column>
<f:facet name="header">UserName</f:facet>
<h:outputText value="#{user.userName}"/>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{user.firstName}"/> <h:outputText
value="#{user.lastName}"/>
</h:column>
</h:dataTable>
</f:view>
</body>
</html>
|
Notice how you bind the dataTable to your Java object.
You then simply define the columns of the table, and the component knows to iterate
over the rows. This assumes a managed backing bean
(UserBean) has been created with your data. The
dataTable component creates the HTML for you, so you
don't have to specify any HTML for the table. This is one of the advantages of
JSF.
JSF has always leveraged JSP technology. However, when it was introduced, JSP 1.2
was the most widely used version of JSP technology. So JSF was based on JSP 1.2,
thus JSF didn't support the JSTL and EL. You might have noticed the value attribute
of the <h:dataTable> and
<h:outputText> tags. These look similar to the EL
used by JSTL. However, in JSF 1.0, these looks are literally only
skin deep. JSF has its own EL that works a lot like the EL introduced by
JSTL, and later made inherent to JSP technology. However, the JSF EL was not
compatible with the JSTL EL—until now.
One of the key points of Java EE 5 is a unified expression language. Thus, the EL used by JSTL and JSF are merged. It's now possible to mix JSTL and JSF, as shown in Listing 8.
Listing 8. JSF and JSTL mix
<f:view>
<c:forEach items="${UserBean.groups}" var="group">
${group.groupName}
<h:dataTable id="#{group.groupId}" value="#{group.users}"
var="user">
<h:column>
<f:facet name="header">UserID</f:facet>
${user.id}
</h:column>
<h:column>
<f:facet name="header">UserName</f:facet>
${user.userName}
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
${user.firstName} ${user.lastName}"
</h:column>
</h:dataTable>
</c:forEach>
</f:view>
|
The example in Listing 8 shows multiple groups that are iterated over using JSTL.
You then create a display table for each group, listing the users in the group.
Notice how the different ELs are used to reference data both for JSTL tags (the
<c:forEach>) and as EL expressions
directly in the JSP component, as well as for JSF components (the
<h:dataTable>). By letting you
mix JSTL and JSF ELs, you get the best of both worlds.
There's one other major change for the EL for Java EE 5. A JSTL implementation wasn't required as part of the J2EE 1.4 specification. Web application developers could choose what JSTL implementation to use with their applications. Of course they could also choose to only use a JSF implementation with their applications.
The Java EE 5 specification requires a JSTL implementation. Web application developers no longer have to worry about including an implementation with their applications. Instead, they can use JSTL without any hesitation. They can also leverage the power of JSF. The unified expression language is inherent to this and is part of the Java EE 5 specification.
The previous section covered the fact that, in the past, Web application developers could choose if they wanted to include JSTL technology. If they did, then it was up to them to pick what JSTL implementation to include with the application. There were various JSTL implementations available to them.
Now with Java EE 5, the JSTL implementation comes with the application server. Thus, any implementation of the Java EE 5 specification must include a JSTL implementation. So when the Apache Geronimo developers started working on their Java EE 5 implementation—Geronimo 2.0—they had to include a JSTL implementation.
However, they couldn't just pick any of the existing implementations. The unified expression language was a major requirement for the JSTL implementation. Many of the JSTL implementations had not been designed to work with JSF. Luckily, the Geronimo team didn't have to implement JSTL and the unified expression language on their own. They were able to leverage Sun's GlassFish.
If you're not familiar with GlassFish, it's Sun's reference implementation of the Java EE 5 specification. It's an open source application, and it's licensed both under Sun's Common Development and Distribution License (CDDL) and the GNU General Public License (GPL). Sun has always provided a reference implementation of JSF, so they were quick to provide a reference implementation of JSTL that included the unified expression language. The open source nature of GlassFish allowed the Geronimo team to leverage this effort and include the GlassFish JSTL implementation with Geronimo 2.0. It was included with the first milestone release for Geronimo: Geronimo 2.0-M1.
Because GlassFish is licensed using both the CDDL and GPL, it can be included with Geronimo. However, its license isn't an Apache-style license like the rest of Geronimo, which placed some limitations on the Geronimo team.
Basically, Geronimo includes GlassFish JSTL but doesn't include the source code for it. Further, the Geronimo team can't alter the source code, although they could obviously contribute to GlassFish JSTL, and then package a new binary with Geronimo.
One of the great things about Geronimo is that you can alter its code and distribute your customized version of Geronimo. However, the JSTL implementation is obviously an exception to this. Its source code is not included with Geronimo, and its licensing places limitations on distributing a customized version of it. For example, if you altered the source of GlassFish, your alterations would have to be made available under the same license as GlassFish.
The evolution of Web technologies in Java technology has greatly benefited developers over the years. The latest evolution, the unified expression language, promises to further benefit developers by allowing them to mix both JSTL and JSF technologies. The unified expression language is now an important part of the Java EE 5 specification, which makes it an important part of Geronimo 2.0. Geronimo has once again helped developers by not only implementing the spec, but by using the reference implementation of the spec. With GlassFish JSTL and Geronimo 2.0, developers have more choices than ever when building Java Web applications.
Learn
- Get a detailed introduction to JSPs in the
tutorial
"Introduction to JavaServer Pages"
(developerWorks, February 2005).
- Read the seminal discussion on Model 2
architecture in the JavaWorld article
"Understanding JavaServer Pages Model 2 Architecture."
- Learn about using JSTL in the article
"JSP best practices: Update your JSP pages with
JSTL"
(developerWorks, May 2003).
- Learn all the things possible with
the expression language in
"A JSTL primer, Part 1: The expression language"
(developerWorks, February 2003).
- Get an introduction to JSF in
"JSF for nonbelievers: Clearing the FUD about JSF"
(developerWorks, February 2005).
- See how the EL and JSF fit in with the rest of
the Java EE stack in
"The Java EE 5
Tutorial."
- Read the latest Geronimo documentation and news on the Geronimo wiki.
- Get involved in the
Geronimo project.
- Join the Apache Geronimo
mailing list.
- Understand what you need to do to apply the
Apache License, Version 2.0.
- Read
"The GlassFish Community Delivering a Java EE Application Server,"
which describes this open source initiative.
- Wikipedia gives a good
overview of the GlassFish project.
- Check out the developerWorks Apache Geronimo project area for articles, tutorials, and other resources to help you get started developing with Geronimo today.
- Find helpful resources for beginners and
experienced users at the Get started now with Apache Geronimo section of developerWorks.
- Check out the IBM® Support for Apache Geronimo offering, which lets you develop Geronimo applications backed by world-class IBM support.
- Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
- Stay current with developerWorks technical events and webcasts.
- Browse all the Apache articles and free Apache tutorials available in the developerWorks Open source zone.
- Browse for books on these and other technical topics at the Safari bookstore.
- Get an RSS feed for this series. (Find out more about RSS.)
Get products and technologies
- Download the latest version of Apache Geronimo.
- Download your free copy
of IBM WebSphere® Application Server Community Edition -- a lightweight J2EE application server built on Apache Geronimo open source technology that is designed to help you accelerate your development and deployment efforts.
- Innovate your next open
source development project with IBM trial software, available for download or on DVD.
Discuss
- Participate in the discussion forum.
- Stay up to date on Geronimo developments at
the Apache Geronimo blog.
- Get involved in the developerWorks community
by participating in developerWorks blogs.





