Define the middle tier for the US State Facts application.
a) Opening states.jsp. Open the states.jsp file that you modified .
b) Defining the form’s method and action attributes in the states.jsp page. In the states.jsp source code, specify "post" as the form element’s method and use the action attribute to forward the client request to the stateFacts.jsp.
c) Populating the HTML menu control with state names. In the states.jsp source code, add a while statement to the scriptlet in states.jsp. The loop should add each state’s name (retrieved from the ResultSet) to the HTML menu control. This step requires you to use literal HTML markup and a JSP expression as well.
d) Closing the ResultSet. In the scriptlet, add code to close the ResultSet by invoking its close method.
e) Saving the file. Save your modified source code file.
f) Opening stateFacts.jsp. Open the stateFacts.jsp that you modified.
g) Displaying the selected state name. In the stateFacts.jsp source code, modify the h1 header element that contains “State Name” to display the selected state name.
h) Moving to the first row in the ResultSet. In the scriptlet that obtains the Result- Set, position the ResultSet cursor to the first row of the ResultSet by calling its next method.
i) Displaying the state’s flag. In the stateFacts.jsp source code, modify the img ele- ment to display the selected state’s flag.
j) Displaying the state’s capital. In the stateFacts.jsp source code, modify the para- graph element that contains “Capital:” to display the selected state’s capital.
k) Displaying the state’s flower. In the stateFacts.jsp source code, modify the paragraph element that contains “Flower:” to display the selected state’s flower.
l) Displaying the state’s tree. In the stateFacts.jsp source code, modify the para- graph element that contains “Tree:” to display the selected state’s tree.
m)Displaying the state’s bird. In the stateFacts.jsp source code, modify the para- graph element that contains “Bird:” to display the selected state’s bird.
n) Closing the ResultSet. In the scriptlet that appears after the code you added in Steps i–m, add code to close the ResultSet by invoking its close method.
o) Saving the file. Save your modified source code file.
p) Copying states.jsp and stateFacts.jsp to the StateFacts directory. Copy your updated states.jsp and stateFacts.jsp files and paste them into the C:\Program Files\Apache Group\Tomcat 4.1\webapps\StateFacts directory.
q) Starting Tomcat. Select Start > Programs > Apache Tomcat 4.1 > Start Tomcat to start the Tomcat server.
r) Testing the application. Open a Web browser and enter the URL http:// localhost:8080/StateFacts/states.jsp, select a state name from the list and click the Review Facts button to test the application.
s) Stopping Tomcat. Select Start > Programs > Apache Tomcat 4.1 > Stop Tomcat to stop the Tomcat server.
```
1
2
3
4 <%-- import java.sql.* for database classes --%>
5 <%@ page import = "java.sql.*" %>
6
7
8
9
10
11
12
13
14
15
16
17
States
18
19
20
92
93
94
```
```
1
2
3
4 <%-- import java.sql.* for database classes --%>
5 <%@ page import = "java.sql.*" %>
6
7
8
9
10
11
12
13
14
15
16
17
<%= request.getParameter( "stateName" ) %>
18
19
20 <%-- begin JSP scriptlet to connect to a database --%>
21 <%
22 // setup database connection
23 try
24 {
25 // specify database location
26 System.setProperty( "db2j.system.home",
27 "C:\\Examples\\Tutorial31\\Exercises\\Databases" );
28
29 // load Cloudscape driver
30 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" );
31
32 // connect to database
33 Connection connection = DriverManager.getConnection(
34 "jdbc:db2j:statefacts" );
35
36 // obtain state information
37 if ( connection != null )
38 {
39 // create statement
40 Statement statement = connection.createStatement();
41
42 // execute query to get state information
43 ResultSet results = statement.executeQuery(
44 "SELECT * FROM states WHERE name = '" +
45 request.getParameter( "stateName" ) + "'" );
46
47 results.next(); // move cursor to the first row
48
49 %> <%-- end scriptlet to insert HTML --%>
50
51
52
53 "State flag for <%= results.getString( "name" ) %>.">
54
55
56
Capital: <%= results.getString( "capital" ) %>
57
58
59
Flower: <%= results.getString( "flower" ) %>
60
61
62
Tree: <%= results.getString( "tree" ) %>
63
64
65
Bird: <%= results.getString( "bird" ) %>
66
67 <% // continue scriptlet
68
69 results.close(); // close result set
70 connection.close(); // close database
71
72 } // end if
73
74 } // end try
75
76 // catch SQLException
77 catch( SQLException exception )
78 {
79 out.println( "Exception: " + exception +
80 }
81
82 %> <%-- end scriptlet --%>
83
84
85
86
87
88
```
You might also like to view...
You would use File Explorer's ________ tab to open or close a file
Fill in the blank(s) with correct word
A(n) ________ fill is a color mixture in which one color fades into another
Fill in the blank(s) with correct word
Match the example with the function or feature:
I. =A54 II. =$A54 III. =$A$54 IV. =SUM(A54:B97) V. "Ohio" A. Function B. Relative reference C. Ensures text entry D. Mixed reference E. Absolute reference
Name three UGUI Components you can use to build your interfaces
What will be an ideal response?