我 2 个文件:
现在我正在对HTML表单进行硬编码,但我希望以HTML格式使用数据库中的数据。
我该怎么做?
Order.jsp
<?xml version="1.0"?>
<%@page contentType="text/xml" %>
<?xml-stylesheet type="text/xsl" href="display.xsl"?>
<%@page import="java.sql.*" %>
<Orders>
<%
// Connection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // load driver
Connection con = DriverManager.getConnection("jdbc:odbc:Project"); // connect DB
// Execute query
Statement s = con.createStatement();
ResultSet rs= s.executeQuery( "SELECT * FROM itemDetail" ); // Execute Query to return data from Statement into ResultSet
while(rs.next()){
%>
<order>
<% out.print("<itemName> "+rs.getString("itemName")+"</itemName><quantity> "+rs.getString("quantity")+ "</quantity><price> "+rs.getString("price")+"</price>");
%>
</order>
<% }
rs.close();
s.close();
con.close();
%>
</Orders>
display.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>Order Page</title></head>
<body>
<h2><b>List of Items available:</b></h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Item Name</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<xsl:for-each select="Orders/order">
<B>------------------------------------------</B>
<br/>
<tr>
Item Name <B><xsl:value-of select="itemName"/></B>
<br/>
Quantity: <I><xsl:value-of select="quantity"/></I>
<br/>
Price: <I><xsl:value-of select="price"/></I>
<br/>
</tr>
<form action="Confirm.jsp" method="POST">
Please select item!
<br/>
<select name="itemName">
<option value="GiftCard">GiftCard</option>
<option value="Chocolate">Chocolate</option>
<option value="Flower">Flower</option>
</select>
<br/>
<br/>
Qty!
<select name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br/>
Price
<input type="text" name="price"/>
<input type="Hidden" name="SessionID" value="1234"/>
<br/>
<input type="submit" value="Add to Cart" />
</form>
</body>
</html>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>