JSP实现的简单购物车详解编程语言

goods_form.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>欢迎光临购物车</title> 
<style type="text/css"> 
h1 { 
	color: #F66; 
} 
</style> 
</head> 
 
<body> 
<div align="center"> 
  <h1> 欢迎光临购物车 </h1> 
  <form name="form1" method="post" action="goods_do.jsp" target=""> 
    <table width="80%" border="0"> 
      <tr> 
        <td width="50%" height="30" align="right"> 请选择您要购买的商品: </td> 
        <td width="50%" height="30" align="left">&nbsp; 
          <select name="GoodsName"> 
            <option value="电脑" selected> 电脑 </option> 
            <option value="MP3"> MP3 </option> 
            <option value="MP4"> MP4 </option> 
            <option value="MP5"> MP5 </option> 
            <option value="洗衣机"> 洗衣机 </option> 
            <option value="电视机"> 电视机 </option> 
          </select></td> 
      </tr> 
      <tr> 
        <td width="50%" height="30" align="right"> 购买数量: </td> 
        <td width="50%" height="30" align="left">&nbsp; 
          <input type="text" name="GoodsNumber" value="1" size="5"></td> 
      </tr> 
    </table> 
    <p> 
      <input type="submit" name="sub" value="提交"> 
      &nbsp;&nbsp; 
      <input type="reset" name="res" value="重购"> 
    </p> 
  </form> 
</div> 
</body> 
</html> 

goods_do.jsp

<%@ page contentType="text/html; charset=utf-8" language="java"%> 
<%@ page import="java.util.*"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
	<head> 
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
		<title>欢迎光临购物车</title> 
		<jsp:useBean id="Goods" scope="session" class="com.demo.Goods" /> 
		<style type="text/css"> 
td { 
	color: #333; 
	font-family: "微软雅黑", Verdana, sans-serif, "宋体"; 
	width: 150px; 
	border-bottom-width: 1px; 
	border-bottom-style: dashed; 
	border-bottom-color: #999; 
} 
 
table { 
	width: 400px; 
	text-align: center; 
	margin-right: auto; 
	margin-left: auto; 
	border: 2px solid #666; 
} 
 
h1 { 
	color: #F66; 
} 
</style> 
	</head> 
	<% 
		//设置编码格式 
		request.setCharacterEncoding("utf-8"); 
		//获取所要添加到购物车的商品名称和数量 
		String sGoodsName = request.getParameter("GoodsName"); 
		String sGoodsNumber = request.getParameter("GoodsNumber"); 
		//根据商品名称是否为空判断是否需要保存商品信息 
		if (sGoodsName != null && sGoodsName != "") { 
			int iGoodsNumber = Integer.parseInt(sGoodsNumber); 
			Goods.add(sGoodsName, iGoodsNumber); 
		} 
 
		//获取购物车对象信息 
		Hashtable h = Goods.show(); 
		//获取购物车中所有商品名称 
		Enumeration e = h.keys(); 
		//keys(),返回此哈希表中的键的枚举。 
	%> 
	<body> 
		<div align="center"> 
			<h1> 
				欢迎光临购物车 
			</h1> 
			<p> 
				您的购物信息如下: 
			</p> 
			<table> 
				<% 
					while (e.hasMoreElements()) { 
						//根据商品名称获得相应商品数量 
						String sTemp = e.nextElement().toString(); 
						int iTemp = ((Integer) h.get(sTemp)).intValue(); 
				%> 
				<tr> 
					<td><%=sTemp%>: 
					</td> 
					<td><%=iTemp%></td> 
					<td> 
						<br> 
						<form action="goods_delete.jsp" method="post"> 
							<input name="deleteName" type="hidden" value="<%=sTemp%>"> 
							<input name="delete" type="submit" value="移除"> 
						</form> 
 
					</td> 
				</tr> 
				<% 
					} 
				%> 
			</table> 
			<p> 
				<input type="button" name="goon" value="继续购物" 
					onClick="javascript:window.location='goods_form.html'"> 
			</p> 
		</div> 
	</body> 
</html> 

goods_delete.jsp

<%@ page contentType="text/html; charset=utf-8" language="java"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
	<head> 
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
		<title>欢迎光临购物车</title> 
		<jsp:useBean id="Goods" scope="session" class="com.demo.Goods" /> 
	</head> 
	<body> 
		<div align="center"> 
			<h1> 
				欢迎光临购物车 
			</h1> 
			<% 
				request.setCharacterEncoding("utf-8"); 
				//获取所要删除的商品名称 
				String sGoodsName = request.getParameter("deleteName"); 
				//删除对应的商品信息 
				Goods.delete(sGoodsName); 
				//跳转当前页面 
				response.sendRedirect("goods_do.jsp"); 
			%> 
		</div> 
	</body> 
</html> 
 
 

Goods.java

package com.demo; 
 
import java.util.*; 
import java.io.*; 
 
/** 
 * 购物车 
 *  
 */ 
public class Goods implements Serializable { 
	public Hashtable Goods = new Hashtable(); 
 
	// 构造函数 
	public void Goods() { 
	} 
 
	// 将某个商品信息加入购物车 
	public void add(String GoodsName, int GoodsNumber) { 
		if (Goods.containsKey(GoodsName)) 
		// containsKey,测试指定对象是否为此哈希表中的键。 
		{// 购物车中存在此商品 
			int iTemp = ((Integer) Goods.get(GoodsName)).intValue(); 
			// intValue(),以 int 类型返回该 Integer 的值。 
			iTemp = iTemp + GoodsNumber; 
			Goods.put(GoodsName, new Integer(iTemp)); 
		} else {// 购物车中不存在此商品 
			Goods.put(GoodsName, new Integer(GoodsNumber)); 
		} 
	} 
 
	// 获取购物车中所有商品 
	public Hashtable show() { 
		return Goods; 
	} 
 
	// 从购物车中删除一件商品 
	public void delete(String GoodsName) { 
		int num = Integer.parseInt(Goods.get(GoodsName).toString()) - 1; 
		if (num == 0) { 
			Goods.remove(GoodsName); 
		} else { 
			Goods.put(GoodsName, num); 
		} 
	} 
} 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10929.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论