slu

Søren's Blog

Random Ramblings

Previous Entry Share Next Entry
HSQLDB Part 4 - JSTL
slu
I'm getting closer and closer to some code, that I would actually recomend using.

I the previous entry I created a crude JSP Page (old fashion style). The page works, but it is ugly and alternates between code and view contexts several times (code is the scriptlets in <% ... %> and view is the HTML). These are well known problems related to all Server Pages technologies. The remidy (when using JSP) is use tag libraries instead of scriptlets (this makes it more pretty to look at), and separate the code into classes (which makes it even more pretty).

Below is a better version of the previous JSP Page. Now as a JSP Document (i.e. it's an XML document). It uses JSTL (JavaServer Pages Standard Tag Library), which means that (almost) all embedded scriptlet code is gone.

<?xml version="1.0" encoding="iso-8859-1"?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"
          xmlns:c="http://java.sun.com/jstl/core"
          xmlns:sql="http://java.sun.com/jstl/sql">

  <jsp:directive.page contentType="text/html; charset=iso-8859-1" />

  <c:set var="title" value="Database Test" />
  <c:set var="database">
    <jsp:scriptlet>
      out.print(application.getRealPath("/database/demobase"));
    </jsp:scriptlet>
  </c:set>

  <sql:setDataSource var="connection"
    url="jdbc:hsqldb:file:${database}" driver="org.hsqldb.jdbcDriver"
    user="sa" password="" />

  <sql:query var="resultSet" dataSource="${connection}">
    SELECT title, url FROM Bookmarks ORDER BY title
  </sql:query>
	  
  <html>
    <head>
      <title><c:out value="${title}" /></title>
    </head>

    <body>

      <h1><c:out value="${title}" /></h1>
  
      <h2>Bookmarks</h2>
    
      <ul>
        <c:forEach var="row" items="${resultSet.rows}">
          <li>
            <c:out value="&lt;a href='${row.url}'&gt;${row.title}&lt;/a&gt;" 
               escapeXml="false" />
          </li>
        </c:forEach>
      </ul>

    </body> 
  </html>

</jsp:root>

?

Log in

No account? Create an account