Home

JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies


JSTL Sample

In comparison to scriptlets, JSTL provides a more elegant approach to your application. You should immediately notice that the pages are cleaner and easier to read.

Customer Listing
Notice that at the very top of the JSP, we use a directive to let the container (Tomcat) know that we are going to use JSTL. Also notice that there is plenty of Expression Language being used within the JSTL to get values that are passed into actions or displayed on the page.

CustomerList-JSTL.jsp

<%– Let the system know that we are using JSTL and load the proper prefixs –%>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core_rt” %>
<%@ taglib prefix=”sql” uri=”http://java.sun.com/jstl/sql_rt” %>

<html>
<head>
<title>JSP and Beyond - Customer List - JSTL Example</title>
</head>
<body>

<h1>Customer List - JSTL Example</h1>

<%– Let the system know what datasource we will speak with –%>
<sql:setDataSource var=”dataSource” url=”jdbc:mysql://YourDatabaseServer/YourDatabaseName” driver=”com.mysql.jdbc.Driver” user=”DatabaseUser” password=”Password” />

<sql:query var=”queryResults” dataSource=”${dataSource}”>
<%– Place our SQL query here within the XML tag “sql:query” –%>
SELECT * FROM tblCustomer ORDER BY CustomerName
</sql:query>

<table width=”100%” border=”1″>
<tr>
<td colspan=”4″>Customer</td>
</tr>

<%– Start a loop through the results from the SQL query and mix it with HTML –%>
<c:forEach var=”row” items=”${queryResults.rows}”>
<tr>
<%– Notice how with each “row” object we can access the column name by specifying it after the “row” object –%>
<td><a href=”CustomerList-JSTL.jsp?shownotes=<c:out value=”${row.CustomerID}”/>”>show notes</a> <c:out value=”${row.CustomerName}”/></td>
<td><a href=”CustomerEdit-JSTL.jsp?CustomerID=<c:out value=”${row.CustomerID}”/>”>edit</a></td>
<td><a href=”NoteAdd-JSTL.jsp?CustomerID=<c:out value=”${row.CustomerID}”/>”>add note</a></td>
<td><a href=”CustomerDelete-JSTL.jsp?CustomerID=<c:out value=”${row.CustomerID}”/>”>delete</a></td>
</tr>
<c:if test=”${(row.CustomerID) == (param.shownotes) or (param.shownotes) == 0}”>
<sql:query var=”queryDetailResults” dataSource=”${dataSource}”>
<%– Place our SQL query here within the XML tag “sql:query” –%>
SELECT * FROM tblNote WHERE CustomerID = ?
<sql:param value=”${row.CustomerID}” />
</sql:query>
<c:forEach var=”rowDetail” items=”${queryDetailResults.rows}”>
<tr><td colspan=”4″><c:out value=”${rowDetail.Note}”/> - <a href=”NoteDelete-JSTL.jsp?NoteID=<c:out value=”${rowDetail.NoteID}”/>”>delete</a></td></tr>
</c:forEach>
</c:if>
</c:forEach>
</table>

<br>
<a href=”CustomerAdd-JSTL.jsp”>Add a New Customer</a>

</body>
</html>

Adding a Customer
Notice that we are using a single page to add a customer. The single page is able to handle this process because EL allows us to check the parameters that are being sent into the page <c:if test=”${(param.action) == null}”>.

CustomerAdd-JSTL.jsp

<%– Let the system know that we are using JSTL and load the proper prefixs –%>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core_rt” %>
<%@ taglib prefix=”sql” uri=”http://java.sun.com/jstl/sql_rt” %>

<html>
<head>
<title>JSP and Beyond - Add Customer - JSTL Example</title>
</head>
<body>

<c:if test=”${(param.action) == null}”>
<h1>Add Customer - JSTL Example</h1>
<form method=”post” action=”<%= request.getRequestURI() %>”>
Customer Name
<input type=”text” name=”CustomerName”>
<input type=”hidden” name=”action” value=”add”>
<br><br>
<input type=”submit” value=”Add Customer”>
</form>
</c:if>
<c:if test=”${(param.action) != null}”>

<c:out value=”${param.CustomerName}” />

<%– Let the system know what datasource we will speak with –%>
<sql:setDataSource var=”dataSource” url=”jdbc:mysql://YourDatabaseServer/YourDatabaseName” driver=”com.mysql.jdbc.Driver” user=”DatabaseUser” password=”Password” />

<sql:update dataSource=”${dataSource}” >
INSERT INTO tblCustomer (CustomerName) VALUES (?)
<sql:param value=”${param.CustomerName}” />
</sql:update>

<h1>Add Customer - Customer Added</h1>
<br>
<a href=”CustomerList-JSTL.jsp”>Main Menu</a>

</c:if>

</body>
</html>