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"> <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"> <input type="text" name="GoodsNumber" value="1" size="5"></td> </tr> </table> <p> <input type="submit" name="sub" value="提交"> <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); } } }
本文由用戶 xybw 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!