Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

JavaServer Pages as Web service clients

In this web services advisor tip, William Brogden discusses the benefits, properties and popularity of JavaServer Pages.

JavaServer Pages, or JSP, are an extension of Java servlet technology designed to facilitate dynamic creation of HTML pages by Web developers without writing Java code. When a JSP-capable server gets a request for a JSP page, it initiates a series of events that result in a Java servlet being executed to produce the response. It is the process by which the JSP source is turned into a servlet that is key to the popularity of JSP with developers.

The JSP source for a page consists of a mix of normal HTML markup plus special "tags." The normal HTML can be thought of as a template that gets filled out by the action of tags. In a two-step process, a specialized JSP compiler turns the JSP source into valid Java servlet code which is then compiled by the normal Java compiler into a servlet class. The server has a map that relates the name of the JSP page to the created servlet. Thus, to the Web designer it looks like the JSP source is being executed, but behind the scenes a lot is going on to create that impression.

Simple "scriptlet" tags allow you to place Java statements right in the middle of HTML markup. You can also declare complete Java methods that will become part of the resulting servlet code. With these tools you can access any of the extensive Java libraries so it is possible -- but not necessarily a good idea -- to build quite complex code in a JSP.

Simple looking JSP tags can cause the creation of extensive amounts of actual Java code, code that would be very tedious to create manually. The downside of this invisible activity is that JSP errors will be harder to debug than normal Java code. Tags that extend the basic JSP syntax are organized into tag libraries known as "taglibs" that must be explicitly imported to a page.

JSP, "Model 1" and "Model 2"

When JSP technology first came into use, programmers gleefully put all sorts of decision making and similar functionality into JSP code. It was easy to do and all your code ended up in one file. Alas, as applications got larger this approach turned out to be hard to debug and maintain, in fact it got downright nightmare-ish.

These days, the all-in-one JSP is referred to as "Model 1" and is only used for the simplest applications where decision making based on the user request is minimal. The preferred architecture, referred to as "Model 2," uses a servlet to examine all user requests, make calculations and decide which JSP should display the result. You may recognize this as the design pattern known as Model-View-Controller (MVC), where JSP provides the HTML "view" appropriate to the request and the servlet is the "controller." Note: the M in MVC refers to the underlying data, not the Model 1 vs. Model 2 distinction.

It is possible, as we will see shortly, to have a JSP page access a Web service directly, parse the results and control the presentation. However, if the page gets much user traffic there may be a lot of inefficient duplication of effort as the same content gets retrieved repeatedly. For all but the simplest low traffic pages, when JSP draw on Web services for data, they should do it indirectly.

The JSP Standard Tag Library

A basic set of tags called the JSP Standard Tag Library (JSTL) has been standardized under the Java Community Process (JSR 52) by multiple industry experts. Oh, if only it was that simple! Actually there are multiple versions of the JSTL, corresponding to the incremental development of the Java Servlet and JSP API standards.

The early version 1.0 of the JSTL corresponds to the Servlet version 2.3 and JSP version 1.2 API specifications. Tomcat 4 is the reference servlet engine that implements this JSP API.

Version 1.1 of the JSTL matches the Servlet version 2.4 and JSP version 2.0 specifications. Tomcat 5 is the reference servlet engine that implements this JSP API.

The latest Java Enterprise Edition 5, has a further revision, version 1.2 of the JSTL. As I understand it, the modification in this next version are to facilitate the integration of the JavaServer Faces application framework with JSP features. Java EE 5 reached final release state about 2 weeks ago.

Using the Standard Tag Library to Access a Service

In order to demonstrate a small fraction of what's possible with JSP I downloaded the JSTL package for JSTL 1.1 from the Apache Software Foundation site (see the resources listed). This package contains much, much more than the JSTL because the open source community has been active in creating custom tag libraries for JSP. Tomcat 5.5.9 was the servlet container.

The Web service we will be using is TechTarget's RSS feed of news headlines related to Web services. Originally RSS stood for Rich Site Summary, but these days, RSS is considered to represent Really Simple Syndication. In any case RSS uses a simple XML file format ideal for parsing using the JSTL. My JSP page starts with two tag formatted lines which specify the taglibs to be used and the namespace prefix that will appear in the tags, plus some standard HTML markup that starts the page.

  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
  <html><head><title>JSTL: XML Demo</title>
  </head><body bgcolor="#FFFFFF">
  <h1>Test RSS Feed from Techtarget</h1>

Next we have the tags that define how to get the source document and parse it into a standard Java XML Document object with a reference stored in a local variable named "document."

  <c:import var="docString" url="http://rss.techtarget.com/80.xml"/>
  <x:parse var="document" doc="${docString}"/> 

Now for the XML parsing action: I have added line numbers to facilitate discussion. Line 1 uses the "set" operation of the xml taglib to set the "channel" variable to the XML document root element. Line 2 inserts the content of the "description" tag into HTML markup for a paragraph. Line 3 is the HTML markup to start formatting an un-numbered list.

    1. <x:set var="channel" select="$document//channel" /> 2. <p>< x:out select="$channel//description"/></p> 3. <ul> 4. <x:forEach select="$channel//item" > 5. <li><a href='<x:out select="link" />' > 6. <x:out select="title"/></a></li> 7. </x:forEach> 8. < /ul>

The RSS "channel" element contains one "item" element for each news headline. Each "item" element has a "title," a "link" to the story on the originating Web site, a long text description and several other elements. A Web designer might use this data in a variety of ways on different JSP. In this case I am sticking to a simple presentation.

We are going to iterate over all of the "item" elements in the document using the forEach tag starting in line 4 and ending in line 7. The format of the "select" attribute follows the familiar XPath conventions to create a nodelist of the item elements. In line 5 we embed the content of the "link" element in HTML markup for a hypertext link. Line 6 uses the content of the "title" element to create the text that will be displayed. Line 8 is the HTML markup to close the list.

Directing a brower to the resulting JSP page hosted in the Tomcat Web server results in a page with a headline "Test RSS Feed from Techtarget," a paragraph containing the RSS description lines and a list of headlines. Each headline being formatted as a clickable link to the appropriate story.


Documentation for the Standard Tag Library at Sun http://java.sun.com/products/jsp/jstl/

The Apache Software Foundation open source Taglibs repository, includes JSTL 1.0 and 1.1 along with many special purpose taglibs. http://jakarta.apache.org/taglibs/

Example of commercial taglib supplier with products free for non commercial use. http://www.servletsuite.com/jsp.htm

Dig Deeper on Topics Archive

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.