代码段3 inventory.jsp
<!--
inventory.jsp -
显示商品目录并且获取用户购买的物品及其数量单价等信息
-->
<html>
<%--
页面的头部,使用另一个html页面,通过include指令来实现
--%>
<%@ include file="header.html" %>
<!--显示标题
-->
<center>
<title> 中国科学技术大学百货商店</title>
<font size="+1">
<b> 欢迎选购我们的商品</b>
</font>
<body bgcolor="#fffff">
<!--
page指令
-->
<%@ page import="shoppingcart.*" errorpage="error.jsp" %>
<%--
创建表单设定页面布局
--%>
<form method="post" action="/shoppingcart/customerservlet">
<table width=450 cellspacing="0" cellpadding="0" border="1">
<%--
创建表头
--%>
<tr>
<td width=5% bgcolor="#eca613"> <b>商品序列号</b></td>
<td bgcolor="#eca613"> <b>商品描述</b></td>
<td width=5% align=center bgcolor="#eca613" > <b>商品数量</b></td>
<td width=25% bgcolor="#eca613"> <b>单价</b> </td>
</tr><%--
声明一个购物篮,和一个product商名目录。
--%>
<%! basketbean basket;
product[] catalogue;
%>
<%--
从httpsession对象中取回当前的购物篮,然后从inventory对象中取回商品列表
--%><%
basket = (basketbean)session.getattribute(basketbean.basket);
catalogue = inventorybean.getcatalogue();
%>
<%--循环显示出product中商品列表中的每一个商品
--%>
<%
for(int i = 0; i < catalogue.length; i++) {
product product = catalogue[i];
%>
<tr>
<td> <%= product.getsku() %> </td>
<td> <%= product.getname() %> </td>
<td> <input type=text size=6 maxlength=6
name="pieces" value=<%= basket.getpieces(product)%>>
</td>
<td align=right> <%= product.getprice() %> </td>
</tr>
<% } %>
<%--
表中显示总共化去多少钱
--%>
<tr>
<td colspan=4 align=right bgcolor="#eca613"><b>
总共人民币<%= basket.getstringifiedvalue(basket.gettotal()) %>元 </b></td>
</tr>
</table>
<%--
发送购买请求
--%>
<table width=450 cellspacing="0" cellpadding="0" border="0">
<tr>
<td align=left><input type=submit
name=<%= basketbean.page %> value=<%= basketbean.update %>></td>
<td align=left><input type=reset value="取消"></td>
<td align=right><input type=submit
name=<%= basketbean.page %> value=<%= basketbean.purchase %>></td>
</tr>
</table>
</form>
</center>
<%@ include file="footer.html" %>
</body>
</html>
receipt.jsp程序中,我们使用了jsp动作,来处理顾客发送来请求的参数值。因此我也想简要的介绍一下jsp中的动作元素。除了指令和脚本元素外,jsp动作也是 jsp页面不可缺少的一部分,一个动作元素有两种基本的语法形式:
<prefix:tag attr_list />
<prefix:tag attr_list>
<body>
</prefix:tag>
当动作有语句体时,我们必须使用后一种表式方法。动作背后基本的概念就是与特定的jsp标签联系的“标签处理器”。这些处理器是基于标签的一些代码段,用来执行某些操作。jsp引擎提供了一些标准的动作,所有的这些动作都要以“ jsp ”为前缀。例如,我们的电子商店使用一个助手bean来简化请求参数分析,我们就要使用<jsp:usebean>元素声明这样一个bean:<jsp:usebean id="receiptbean" scope="request" class="shoppingcart.receiptbean" /> jsp声明了一个对象变量,取名为receiptbean,作为 receiptbean 的一个实例,在当前请求完成时就会被释放。使用bean的主要优点就是它分析并且返回 html请求参数的简洁性。
在声明完bean以后,我们就可以使用<jsp:setproperty>元素从html请求中获取参数,来设置它的属性值。我们可以显式的指出属性和html 参数的名字,来设置属性值。例如,下面是receipt.jsp中用来设置我们的receiptbean实例的属性的一些语句:<jsp:setproperty name="receipt_bean" property="name" param="name" /> 如果我们的属性名和相应的参数名相同,我们可以指示用一个jsp元素来设置所有的属性:<jsp:setproperty name="receipt_bean" property="*" /> 这个单独的元素告诉引擎,使用 java映像来匹配所有的jsp参数和javabean属性名,然后使用html请求信息中的值来设置javabean的属性值。同样,我们使用<jsp:getproperty>元素从助手bean中返回属性值。例如,下面是我们返回属性的语句:<jsp:getproperty name="receipt_bean" property="name" /> 在receiptbean类的代码中,每个在receipt.jsp中被分析的参数,在我们程序bean中都有一个相关联的用来设置了取得的方法:例如,<jsp:setproperty name="receipt_bean" property="name" param="name" />有一个相关联的设置方法:void setname(string phone);同样, <jsp:getproperty name="receipt_bean" property="name" />也有一个相关联的取得方法:string getname();
代码段4:receipt.jsp
<%@ page import="shoppingcart.*" errorpage="error.jsp" %>
<html>
<title> 中国科学技术大学百货商店收银台</title>
<body bgcolor="#fffff">
<%@ include file="header.html" %>
<center>
<font size="+1"> <b> 顾客信息</b> </font>
<form method="post" action="/shoppingcart/customerservlet">
<table width=450 cellspacing="0" cellpadding="0" border="1">
<tr>
<td width=10% bgcolor="#eca613"> <b>姓名:</b></td>
<td width=90%> <input type=text size=50 name="name" ></td>
</tr>
<tr>
<td width=10% bgcolor="#eca613"> <b>email: </b></td>
<td width=90%> <input type=text size=50 name="email"></td>
</tr>
<tr>
<td width=10% bgcolor="#eca613" > <b>地址</b></td>
<td width=90%> <input type=text size=50 name="address"></td>
</tr>
<tr>
<td width=10% bgcolor="#eca613"> <b>电话</b> </td>
<td width=90%> <input type=text size=50 name="phone"></td>
</tr>
</table>
<%
basketbean basket = (basketbean)session.getattribute(basketbean.basket);
%>
<b>
总共价格人民币<%= basket.getstringifiedvalue(basket.gettotal()) %>元
</b>
<table width=450 cellspacing="0" cellpadding="0" border="0">
<tr>
<td align=left><input type=reset value="reset"></td>
<td align=right><input type=submit name=<%= basketbean.page %> value=<%= basketbean.receipt %> ></td>
</tr>
</table>
</form>
</center>
<%@ include file="footer.html" %>
</body>
</html>
代码段5:confirm.jsp
<!--
confirm.jsp -
确认顾客购买商品,并且显示账单
-->
<html>
<!--
页眉
-->
<%@ include file="header.html" %>
闽公网安备 35060202000074号