中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

JSP實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
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>歡迎光臨購(gòu)物車</title>
<style type="text/css">
h1 {
	color: #F66;
}
</style>
</head>

<body>
<div align="center">
  <h1> 歡迎光臨購(gòu)物車 </h1>
  <form name="form1" method="post" action="goods_do.jsp" target="">
    <table width="80%" border="0">
      <tr>
        <td width="50%" height="30" align="right"> 請(qǐng)選擇您要購(gòu)買的商品: </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="洗衣機(jī)"> 洗衣機(jī) </option>
            <option value="電視機(jī)"> 電視機(jī) </option>
          </select></td>
      </tr>
      <tr>
        <td width="50%" height="30" align="right"> 購(gòu)買數(shù)量: </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="重購(gòu)">
    </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>歡迎光臨購(gòu)物車</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>
	<%
		//設(shè)置編碼格式
		request.setCharacterEncoding("utf-8");
		//獲取所要添加到購(gòu)物車的商品名稱和數(shù)量
		String sGoodsName = request.getParameter("GoodsName");
		String sGoodsNumber = request.getParameter("GoodsNumber");
		//根據(jù)商品名稱是否為空判斷是否需要保存商品信息
		if (sGoodsName != null && sGoodsName != "") {
			int iGoodsNumber = Integer.parseInt(sGoodsNumber);
			Goods.add(sGoodsName, iGoodsNumber);
		}

		//獲取購(gòu)物車對(duì)象信息
		Hashtable h = Goods.show();
		//獲取購(gòu)物車中所有商品名稱
		Enumeration e = h.keys();
		//keys(),返回此哈希表中的鍵的枚舉。
	%>
	<body>
		<div align="center">
			<h1>
				歡迎光臨購(gòu)物車
			</h1>
			<p>
				您的購(gòu)物信息如下:
			</p>
			<table>
				<%
					while (e.hasMoreElements()) {
						//根據(jù)商品名稱獲得相應(yīng)商品數(shù)量
						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="繼續(xù)購(gòu)物"
					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>歡迎光臨購(gòu)物車</title>
		<jsp:useBean id="Goods" scope="session" class="com.demo.Goods" />
	</head>
	<body>
		<div align="center">
			<h1>
				歡迎光臨購(gòu)物車
			</h1>
			<%
				request.setCharacterEncoding("utf-8");
				//獲取所要?jiǎng)h除的商品名稱
				String sGoodsName = request.getParameter("deleteName");
				//刪除對(duì)應(yīng)的商品信息
				Goods.delete(sGoodsName);
				//跳轉(zhuǎn)當(dāng)前頁(yè)面
				response.sendRedirect("goods_do.jsp");
			%>
		</div>
	</body>
</html>



Goods.java
package com.demo;

import java.util.*;
import java.io.*;

/**
 * 購(gòu)物車
 * 
 */
public class Goods implements Serializable {
	public Hashtable Goods = new Hashtable();

	// 構(gòu)造函數(shù)
	public void Goods() {
	}

	// 將某個(gè)商品信息加入購(gòu)物車
	public void add(String GoodsName, int GoodsNumber) {
		if (Goods.containsKey(GoodsName))
		// containsKey,測(cè)試指定對(duì)象是否為此哈希表中的鍵。
		{// 購(gòu)物車中存在此商品
			int iTemp = ((Integer) Goods.get(GoodsName)).intValue();
			// intValue(),以 int 類型返回該 Integer 的值。
			iTemp = iTemp + GoodsNumber;
			Goods.put(GoodsName, new Integer(iTemp));
		} else {// 購(gòu)物車中不存在此商品
			Goods.put(GoodsName, new Integer(GoodsNumber));
		}
	}

	// 獲取購(gòu)物車中所有商品
	public Hashtable show() {
		return Goods;
	}

	// 從購(gòu)物車中刪除一件商品
	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);
		}
	}
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:倒計(jì)時(shí)的一段JS腳本

下一篇:HTML5 上傳前預(yù)覽