Skip to main content

More Easy JSTL

Posted by bleonard on September 30, 2005 at 11:34 AM PDT

Yesterday I put together a blog showing how easy it is to work with JSTL in NetBeans. One thankful reader asked for a follow-up on using the JSTL XML library. I think this was primarily due to a problem he was having with a package name change from JDK1.4 to 5 (see my response for more details). But it's simple enough, so here goes anyhow. I'll basically just do what I did yesterday, display a table of customers. However, today, that customer list will come from and XML document rather than a SQL database.

<?xml version="1.0" encoding="UTF-8"?>
        <name>Livingston Enterprises</name>
        <name>Oak Computers</name>

I didn't mention yesterday, but this exercise was done with NetBeans 4.1. You could also use the NetBeans 5.0 BETA .

  1. Start NetBeans
  2. Create a new Web Application named CustomerListXML (File > New Project > Web > Web Application)
  3. Add the JSTL library to the project (Properties > Libraries > Add Library)
  4. Uncomment the <%@taglib uri="" prefix="c"%> line in index.jsp
  5. Add the line <%@taglib uri="" prefix="x"%>
  6. Insert the following between the tags:
 <h1>Customer List</h1>
<!-- Parse the XML Customer List -->
<c:import url="customers.xml" var="xml"/>
<x:parse xml="${xml}" var="customers"/>

<TABLE border=1>
         <TD>Customer Num</TD>
         <TD>Customer Name</TD>
         <TD>Customer City</TD>
         <TD>Customer Phone</TD>
     <x:forEach select="$customers/customers/customer" var="customers">
             <TD><x:out select="num"/></TD>
             <TD><x:out select="name"/></TD>
             <TD><x:out select="city"/></TD>
             <TD><x:out select="phone"/></TD>
  1. If you actually took the time to type in the code, you should have noticed the very helpful JSTL code assistance and documentation:


  2. If you pasted in the code from above, Press Ctrl+Shift+F to reformat the code.
  3. Add a new Well-formed XML Document named "customers" to the Web Pages folder (New > File/Folder > XML Documents) and replace the contents with the XML from above.
  4. Now, if you're developing on JDK 1.4 OR using the J2EE 1.4 RI (Sun Java System Application Server 8.1) as your runtime, press F6 to test your application. If you're developing on JDK 5.0 AND running on Tomcat, you also need to add xalan.jar to your project (Project > Libraries > Add JAR/Folder) before pressing F6 to run it.

Related Topics >>