Build a Servlet-Based Application that Executes SQL Statements Against a Database - The Code for the JSP
(Page 4 of 7 )
The code for the JSP starts with a scriptlet that contains Java code that retrieves two attributes from the session object:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<%
String sqlStatement =
(String) session.getAttribute("sqlStatement");
if (sqlStatement == null)
sqlStatement = "";
String message =
(String) session.getAttribute("message");
if (message == null)
message = "";
%>
The first attribute is the string that contains the SQL statement, and the second attribute is the string that contains the result message. If these attributes contain null values, they haven't been set yet so this code sets the sqlStatement and message variables to empty strings.
This JSP also contains an HTML form that contains a text area and a submit button:
<form action="../servlet/murach.sql.SQLGatewayServlet" method="post">
<b>SQL statement:</b><br>
<textarea name="sqlStatement" cols=60 rows=8>
<%= sqlStatement %>
<textarea><br>
<br>
<input type="submit" value="Execute">
<form>
Here, the text area allows the user to enter the SQL statement. This code creates a text area that's approximately 60 characters wide and 8 lines tall. Within this area, the sqlStatement variable is displayed, which is empty the first time this JSP is run. Then, when the user clicks the submit button, this JSP calls the SQLGatewayServlet that's described later in this article.
The table near the end of the JSP displays the message string that contains the result of the SQL statement:
<b>SQL result:</b><br>
<table cellpadding="5" border="1">
<%= message %>
<table>
Since this message contains the rows and columns for an HTML table, it's coded within the Table tags.
Next: The Code for the Servlet >>
More Java Articles
More By Joel Murach