Complete Code Listings
Now that we've see how the code works and how to build shopping-cart functionality with WAP, you can reference the complete code listings in the following section. The code listings can also be found on the CD-ROM that accompanies this book.
login.jsp
Listing 18.1 The login.jsp File Creates the Login Page
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"
"http://www.wapforum.org/DTD/wml12.dtd">
<% response.setContentType("text/vnd.wap.wml"); %>
<%@ page import="java.sql.*, javax.servlet.http.Cookie" %>
<%
Connection dbConn = null;
int CartID=-1;
ResultSet get_cart = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
Statement userStmt = dbConn.createStatement();
get_cart = userStmt.executeQuery(
"SELECT VALUE from Vars WHERE Name='CART'"
);
if (get_cart.next()) {
CartID = get_cart.getInt("Value");
userStmt.executeUpdate(
"UPDATE Vars SET [Value]=[Value]+1 WHERE Name='CART'"
);
}
else
{
CartID=1;
userStmt.executeUpdate(
"INSERT INTO Vars(Name, [Value]) SELECT 'CART'AS EXPR1, 2 as EXPR2"
);
}
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
// assume no error conditions, for now
}
%>
<wml>
<card id="Welcome" title="Welcome">
<p>
<b>Burgerworld.com</b>
Burgers, Fries, Soda, and Computers... to go<br/>
<a href="main.jsp?CartID=<%=CartID%>">Enter</a>
</p>
</card>
</wml >
main.jsp
Listing 18.2 The main.jsp File Drives Dynamic Catalog Content to the User
<% response.setContentType("text/vnd.wap.wml"); %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"
"http://www.wapforum.org/DTD/wml12.dtd">
<%@ page import="java.sql.*, javax.servlet.http.Cookie" %>
<%
Connection dbConn = null;
ResultSet get_cat = null;
ResultSet get_products = null;
boolean isMore = false;
// get a connection to the database
try
{
// instantiate the db driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// setup the connection
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
// create a statement on the connection
Statement userStmt = dbConn.createStatement();
// issue the SQL statements
get_cat = userStmt.executeQuery(
"SELECT Distinct Item_Category from Items"
);
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
// assume no error conditions, for now
}
%>
<wml>
<card id="MainMenu" title="Menu">
<p>
Burgerworld.com<br/>
<%
try
{
while (get_cat.next())
{
String Category = get_cat.getString ("Item_Category");
%>
<a href="#<%=Category%>"><%= Category %></a><br/>
<%
}
}
catch (SQLException e)
{
}
%>
</p>
</card>
<%
ResultSet get_catcard = null;
ResultSet get_Items = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
Statement userStmt = dbConn.createStatement();
get_catcard = userStmt.executeQuery(
"SELECT Distinct Item_Category from Items"
);
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
}
try
{
while (get_catcard.next())
{
String CardCategory = get_catcard.getString("Item_Category");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
Statement userStmt = dbConn.createStatement();
get_Items = userStmt.executeQuery(
"SELECT * FROM Items WHERE Item_Category='" + CardCategory + "'"
);
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
}
%>
<card id="<%= CardCategory %>" title="Order <%= CardCategory%>">
<p>
<b><%= CardCategory %></b><br/>
<%
while (get_Items.next())
{
%>
<a href="orderproduct.jsp?CartID=<%=request.getParameter("CartID") %>&ItemID=<
%=get_Items.getString("Item_ID")%>"><%=get_Items.getString ("Item_Name")%></a><br/>
<% } %>
</p>
</card>
<% }
}
catch (SQLException e)
{
}
%>
</wml >
orderproduct.jsp
Listing 18.3 The orderproduct.jsp File Allows the User to Verify the Item and Specify a Quantity
<% response.setContentType("text/vnd.wap.wml"); %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"
"http://www.wapforum.org/DTD/wml12.dtd">
<%@ page import="java.sql.*, javax.servlet.http.Cookie" %>
<%
Connection dbConn = null;
ResultSet get_Item = null;
// get a connection to the database
try
{
// instantiate the db driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// setup the connection
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
// create a statement on the connection
Statement userStmt = dbConn.createStatement();
// issue the SQL statements
get_Item = userStmt.executeQuery(
"SELECT * from Items where Item_ID=" + request.getParameter("ItemID")
);
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
// assume no error conditions, for now
}
%>
<wml>
<card id="OrderIt" title="Order Product">
<do type="accept" label="Order">
<go href="addtocart.jsp">
<postfield name="Quantity" value="$Quantity"/>
<postfield name="CartID" value="<%= request.getParameter("CartID")%>"/>
<postfield name="ItemID" value="<%= request.getParameter("ItemID")%>"/>
</go>
</do>
<p>
<% try
{
if (get_Item.next())
{
%>
<b><%= get_Item.getString("Item_Name")%></b><br/>
Cost <%= get_Item.getString("Item_Cost")%><br/>
SKU #<%= get_Item.getString("Item_SKU")%><br/>
Quantity : <input type="text" name="Quantity" format="N*"/>
<% }
}
catch (Exception e)
{
}
%>
</p>
</card>
</wml >
addtocart.jsp
Listing 18.4 The addtocart.jsp File Adds the Selected Item to the User's Cart and Prompts for "Checkout"
<% response.setContentType("text/vnd.wap.wml"); %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"
"http://www.wapforum.org/DTD/wml12.dtd">
<%@ page import="java.sql.*, javax.servlet.http.Cookie" %>
<%
Connection dbConn = null;
ResultSet get_Item = null;
// get a connection to the database
try
{
// instantiate the db driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// setup the connection
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
// create a statement on the connection
Statement userStmt = dbConn.createStatement();
// issue the SQL statements
userStmt.executeUpdate(
"INSERT INTO ShoppingCart_Items(ShoppingCartID, Item, Quantity) SELECT " +
request.getParameter("CartID") + " AS EXPR1, " + request.getParameter("ItemID") + " as
EXPR2, " + request.getParameter ("Quantity")
);
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
// assume no error conditions, for now
}
%>
<wml>
<card id="OrderIt" title="Order Product">
<p>
<b>The item is in your cart!</b><br/>
<a href="main.jsp?CartID=<%=request.getParameter ("CartID")%>">Shop More</a>
<a href="checkout.jsp?CartID=<%=request.getParameter ("CartID")%>">Check Out</a>
</p>
</card>
</wml >
checkout.jsp
Listing 18.5 The checkout.jsp File Lists All Ordered Items and Allows the User to Enter His Shipping and Ordering Data
<% response.setContentType("text/vnd.wap.wml"); %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"
"http://www.wapforum.org/DTD/wml12.dtd">
<%@ page import="java.sql.*, javax.servlet.http.Cookie" %>
<%
Connection dbConn = null;
ResultSet getCart = null;
// get a connection to the database
try
{
// instantiate the db driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// setup the connection
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
// create a statement on the connection
Statement userStmt = dbConn.createStatement();
// issue the SQL statements
getCart = userStmt.executeQuery(
"SELECT * FROM ShoppingCart_Items INNER JOIN Items ON ShoppingCart_Items
.Item=Items.Item_ID WHERE ShoppingCartID=" + request.getParameter("CartID")
);
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
// assume no error conditions, for now
}
%>
<wml>
<card id="Checkout" title="Checkout">
<p>
<b>Your order:</b><br/>
<% try
{
while (getCart.next())
{
%>
<%=getCart.getString("Quantity")%> : <%= getCart.getString("Item_Name")%><br/>
<%
}
}
catch (Exception e)
{
}
%>
<a href="#add">Continue</a>
</p>
</card>
<card id="add" title="Add an appointment">
<do type="accept" label="Add">
<go href="finishorder.jsp">
<postfield name="address" value="$Address"/>
<postfield name="creditcard" value="$CreditCard"/>
<postfield name="CartID" value="<%= request.getParameter("CartID")%>"/>
</go>
</do>
<p>
<b>Order Information</b><br/>
Address: <input type="text" name="Address"/>
Credit Card: <input type="text" name="CreditCard" />
</p>
</card>
</wml >
finishorder.jsp
Listing 18.6 The finishorder.jsp File Completes the User's Order and Adds It to the Order Database Table
<% response.setContentType("text/vnd.wap.wml"); %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"
"http://www.wapforum.org/DTD/wml12.dtd">
<%@ page import="java.sql.*, javax.servlet.http.Cookie" %>
<%
Connection dbConn = null;
// get a connection to the database
try
{
// instantiate the db driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// setup the connection
dbConn = DriverManager.getConnection(
"jdbc:odbc:shop"
);
// create a statement on the connection
Statement userStmt = dbConn.createStatement();
// issue the SQL statements
userStmt.executeUpdate(
"INSERT INTO Orders(Username, Address, Phone, CreditCard, OrderID) SELECT 'WEB'as
EXPR1, '" + request.getParameter("Address") + "'AS EXPR2, '555-1212'as EXPR3, '" + request
.getParameter("CreditCard") + "'as expr4, " + request.getParameter("CartID") + " as expr5"
);
userStmt.executeUpdate(
"INSERT INTO Orders_Products(OrderID, ItemID, Quantity) SELECT ShoppingCartID,
Item, Quantity FROM ShoppingCart_Items WHERE ShoppingCartID=" + request.getParameter("CartID")
);
}
catch(Exception e)
{
out.println("Exception!!!");
out.println(e.getMessage());
return;
// assume no error conditions, for now
}
%>
<wml>
<card id="Notify" title="NotifyUser">
<p>
<b>Thanks for your order!</b><br/>
<a href="login.jsp">Place a new order</a>
</p>
</card>
</wml >
..................Content has been hidden....................
You can't read the all page of ebook, please click
here login for view all page.