Home

JSP and Beyond

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


Scriptlet Sample

Scriptlets embed Java code directly within the page markup. Although unquestionably useful and powerful, this model makes management of the presentation layer difficult since the program logic becomes interspersed throughout the presentation code.

Customer Listing
In the customer listing below, you can see that database connectivity code in addition to some HTML markup that is programmatically created, exist within this JSP. This makes it a bit difficult to read or adjust if you wanted to look directly at the user interface portion of what was going to be ultimately produced.

CustomerList-Scriptlet.jsp

<%@page import=”java.sql.*” %>

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

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

<%
Connection conn = null;

// Reference the JDBC driver
Class.forName(”com.mysql.jdbc.Driver”).newInstance();
conn = DriverManager.getConnection(”jdbc:mysql:///YourDatabaseName”,”DatabaseUser”, “Password”);
Statement stmt = conn.createStatement();

// Set the SQL query
ResultSet rs = stmt.executeQuery(”SELECT * FROM tblCustomer ORDER BY CustomerName”);

// Start our table tag
out.println(”<table width=\”100%\” border=\”1\”>”);
out.println(”<tr><td colspan=\”4\”>Customer</td></tr>”);

while(rs.next())
{
out.println(”<tr><td>”);

// Render a link that will let this page show notes
out.println(”<a href=\”" + request.getRequestURI() + “?shownotes=” + rs.getString(”CustomerID”) + “\”>show notes</a>”);

// Render the Customer name
out.println(rs.getString(”CustomerName”) + “</td>”);

// Render the customer type
//out.println(rs.getString(”CustomerType”) + “</td><td>”);

// Render edit link
out.println(”<td><a href=\”CustomerEdit-Scriptlet.jsp?CustomerID=” + rs.getString(”CustomerID”) + “\”>edit</td>”);

// Render add note link
out.println(”<td><a href=\”NoteAdd-Scriptlet.jsp?CustomerID=” + rs.getString(”CustomerID”) + “\”>add note</td>”);

// Render delete customer link
out.println(”<td><a href=\”CustomerDelete-Scriptlet.jsp?CustomerID=” + rs.getString(”CustomerID”) + “\”>delete</td>”);

out.println(”</tr>”);

// Check to see if show details is enabled
if (request.getParameter(”shownotes”) != null)
{
if (request.getParameter(”shownotes”).equals(rs.getString(”CustomerID”)) | request.getParameter(”shownotes”).equals(”all”))
{

Statement stmtDetails = conn.createStatement();

// Set the SQL query
ResultSet rsDetails = stmtDetails.executeQuery(”SELECT * FROM tblNote WHERE CustomerID = ” + rs.getString(”CustomerID”));

while(rsDetails.next())
{
// Render the customer type
out.println(”<tr><td colspan=\”4\”>” + rsDetails.getString(”Note”) + ” - <a href=\”NoteDelete-Scriptlet.jsp?NoteID=” + rsDetails.getString(”NoteID”) + “\”>delete</a></td></tr>”);
}

rsDetails.close();
stmtDetails.close();
}
}
}

// Close our table
out.println(”</table>”);

// Close the connections to database objects to free resources
stmt.close();
conn.close();
%>

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

</body>
</html>

Deleting a Customer
The following code uses inline Java to connect with the database and delete a customer account. You can see that the majority of the code is spent dealing with JDBC calls.

CustomerDelete-Scriptlet.jsp

<%@page import=”java.sql.*” %>

<html>
<head>
<title>JSP and Beyond - Delete Customer - Scriptlet Example</title>
</head>
<body>
<%
if (request.getParameter(”CustomerID”) != null)
{
try {
Connection conn = null;

// Reference the JDBC driver
Class.forName(”com.mysql.jdbc.Driver”).newInstance();
conn = DriverManager.getConnection(”jdbc:mysql:///YourDatabaseName”,”DatabaseUser”, “Password”);
Statement stmt = conn.createStatement();

String sCustomerID = request.getParameter(”CustomerID”).toString();

stmt.executeUpdate(”DELETE FROM tblCustomer WHERE CustomerID = ” + sCustomerID);

// Close any connectivity that we had to the database to free resources
stmt.close();
conn.close();
}
catch (Exception ex)
{
out.println(”An Error has Occured: ” + ex.toString());
}
%>
<h1>Delete Customer - Customer Deleted</h1>
<br>
<a href=”CustomerList-Scriptlet.jsp”>Main Menu</a>
<%
}
%>

</body>
</html>