用JSP实现的商城购物车模块

用JSP实现的商城购物车模块

 

这两天,在学习JSP,正好找个小模块来练练手:

    下面就是实现购物车模块的页面效果截图:

<span>用JSP实现的商城购物车模块</span>

图1. 产品显示页面 通过此页面进行产品选择,添�到购物车

 

<span>用JSP实现的商城购物车模块</span>

图2 .购物车页面

 

<span>用JSP实现的商城购物车模块</span>

图3 . 商品数量设置

 

好了,先不贴图了,直接上代码;先看看项目的文档结构把(麻雀虽小,五脏俱全):

 

<span>用JSP实现的商城购物车模块</span>

整个项目包括三个类,两个JSP页面,以下分别把他们的代码贴上:

 

Cart.java

package shopping.cart;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class Cart {
	List<CartItem> items = new ArrayList<CartItem>();

	public List<CartItem> getItems() {
		return items;
	}

	public void setItems(List<CartItem> items) {
		this.items = items;
	}
	
	public void add(CartItem ci) {
		for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) {
			CartItem item = iter.next();
			if(item.getProduct().getId() == ci.getProduct().getId()) {
				item.setCount(item.getCount() + 1);
				return;
			}
		}
		
		items.add(ci);
	}
	
	public double getTotalPrice() {
		double d = 0.0;
		for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
			CartItem current = it.next();
			d += current.getProduct().getPrice() * current.getCount();
		}
		return d;
	}
	
	public void deleteItemById(String productId) {
		for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) {
			CartItem item = iter.next();
			if(item.getProduct().getId().equals(productId)) {
				iter.remove();
				return;
			}
		}
	}
	
}

 

CartItem.java

 

package shopping.cart;

import shopping.cart.Product;

public class CartItem {
	private Product product;

	private int count;

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
}

 

 Product.java

package shopping.cart;
import java.io.Serializable;

public class Product implements Serializable {
	private String id;// 产品标识
	private String name;// 产品名称
	private String description;// 产品描写叙述
	private double price;// 产品价格

	public Product() {
	}

	public Product(String id, String name, String description, double price) {
		this.id = id;
		this.name = name;
		this.description = description;
		this.price = price;
	}


	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public double getPrice() {
		return price;
	}
}

 

以下是俩JSP页面源代码:

ShowProducts.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="shopping.cart.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		
		<title>My JSP 'ShowProductsJSP.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		
	</head>

	<body bgcolor="#ffffff">
		<%
			Map products = new HashMap();
			products.put("1", new Product("1", "mp3播放器",
					"效果非常不错的mp3播放器,存储空间达1GB", 100.00));
			products.put("2", new Product("2", "数码相机", "象素500万,10倍光学变焦",
					500.00));
			products.put("3", new Product("3", "数码摄像机",
					"120万象素,支持夜景拍摄,20倍光学变焦", 200.00));
			products.put("4", new Product("4", "迷你mp4",
					"市面所能见到的最好的mp4播放器,国产", 300.00));
			products.put("5", new Product("5", "多功能手机",
					"集mp3播放、100万象素数码相机,手机功能于一体", 400.00));
			products.put("6", new Product("6", "多功能手机111",
					"集mp3播放23、100万象素数码相机111,手机功能于一体111",600.00));
			products.put("7", new Product("7", "11111111111",
					"集mp3播放23、100万象素数码相机111,手机功能于一体111",700.00));
			products.put("8", new Product("8", "2222222222",
					"集mp3播放23、100万象素数码相机111,手机功能于一体111",800.00));
			products.put("9", new Product("9", "33333333333333",
					"集mp3播放23、100万象素数码相机111,手机功能于一体111",900.00));
			session.setAttribute("products", products);
		%>
		<H1>
			产品显示
		</H1>

		<form name="productForm"
			action="http://localhost:8088/JSPlearning/ShopCartJSP.jsp"
			method="POST">
			<input type="hidden" name="action" value="purchase">
			<table border="1" cellspacing="0">
				<tr bgcolor="#CCCCCC">
					<tr bgcolor="#CCCCCC">
						<td>
							序号
						</td>
						<td>
							产品名称
						</td>
						<td>
							产品描写叙述
						</td>
						<td>
							产品单位价格(¥)
						</td>
			
                        <td>
							加入�到购物车
						</td>
					</tr>
					<%
						Set productIdSet = products.keySet();
						Iterator it = productIdSet.iterator();
						int number = 1;

						while (it.hasNext()) {
							String id = (String) it.next();
							Product product = (Product) products.get(id);
					%><tr>
						<td>
							<%=number++%>
						</td>
						<td>
							<%=product.getName()%>
						</td>
						<td><%=product.getDescription()%>
						</td>
						<td>
							<%=product.getPrice()%></td>
						<td>
							<a href="Buy.jsp?id=<%=product.getId()%>&action=add" target="cart">我要购买</a>
						</td>
					</tr>
					<%
						}
					%>
				
			</table>
			<p>
			</p>
		</form>
	</body>
</html>

 

Buy.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="shopping.cart.*" %>

<%
Cart c = (Cart)session.getAttribute("cart");
if(c == null) {
	c = new Cart();
	session.setAttribute("cart", c);
}

double totalPrice = c.getTotalPrice();

request.setCharacterEncoding("GBK");
String action = request.getParameter("action");

Map products = (HashMap)session.getAttribute("products");

if(action != null && action.trim().equals("add")) {
	String id = request.getParameter("id");
	Product p = (Product)products.get(id);
	CartItem ci = new CartItem();
	ci.setProduct(p);
	ci.setCount(1);
	c.add(ci);
}

if(action != null && action.trim().equals("delete")) {
	String id = request.getParameter("id");
	c.deleteItemById(id);
}

if(action != null && action.trim().equals("update")) {
	for(int i=0; i<c.getItems().size(); i++) {
		CartItem ci = c.getItems().get(i);
		int count = Integer.parseInt(request.getParameter("p" + ci.getProduct().getId()));
		ci.setCount(count);
	}
}
 %> 

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
List<CartItem> items = c.getItems();
%>

<!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=GB18030">
<title>购物车</title>

</head>
<body>

<!-- c的值是:<%=(c == null) %> items的值是:<%=(items == null) %> -->
<form action="Buy.jsp" method="get">
<input type="hidden" name="action" value="update"/>
<table align="center" border="1">
	<tr>
		<td>产品ID</td>
		<td>产品名称</td>
		<td>购买数量</td>
		<td>单位价格</td>
		<td>总价</td>
		<td>处理</td>
	</tr>
	<%
	for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
		CartItem ci = it.next();
		%>
		<tr>
			<td><%=ci.getProduct().getId() %></td>
			<td><%=ci.getProduct().getName() %></td>
			<td>
				<input type="text" size=3 name="<%="p" + ci.getProduct().getId() %>" value="<%=ci.getCount() %>" 
					onkeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"
					onchange="document.forms[0].submit()">				
			</td>
			<td><%=ci.getProduct().getPrice() %></td>
			<td><%=ci.getProduct().getPrice()*ci.getCount() %></td>
			<td>
				
				<a href="Buy.jsp?action=delete&id=<%=ci.getProduct().getId() %>">删除</a>
				
			</td>
		</tr>
		<%
	}
	%>
	
	
	<tr>
		<td colspan=3 align="right">
			全部商品总价格为:
		</td>
		<td colspan=3><%=c.getTotalPrice() %></td>
	</tr>
	
	
	<tr>
	<!--	<td colspan=3>
			 <a href="javascript:document.forms[0].submit()">改动</a> 
		</td>  -->
		<td colspan=6 align="right">
			<a href="">下单</a>		
		</td>
	</tr>
</table>
</form>
</body>
</html>

 

配置好相关文件,在tomcat中公布后,在浏览器中输入http://localhost:8088/shopCart/ShowProducts.jsp 就可以进入产品展示页面,其他操作都能够在页面上完毕!

注意:我使用的tomcatport(8088)被自己改过,假设没改过tomcatport的童鞋,默认port为8080。

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119049.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 大疆网上测评题库_大疆笔试题

    大疆网上测评题库_大疆笔试题大疆笔试的体验很好,没有很为难应聘者,还有着自己鲜明的特点,我认为值得一说,特此写笔经记录一下,顺便攒攒RP,第一次笔经就献给大疆啦~笔试网站是大疆自己搭建的(UI设计炒鸡好看!!!),我猜题目也是大疆HR团队自己出的。从这点来看,大疆对人才的把控很严格,必须是自己经手选出来的人。整套笔试题目共有90道题,给了1.5个小时完成,题型包括态度行为题、行业知识题、工作情景题,以及略有升级的行测题。其中…

    2022年6月30日
    523
  • leetcode516_leetcode46

    leetcode516_leetcode46题目描述Givenacollectionofnumbers,returnallpossiblepermutations.Forexample,[1,2,3]havethefollowingpermutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],and[3,2,1].分析代码publicList<

    2022年9月20日
    3
  • 鼠标移到导航上面 当前的LI变色 处于当前的位置

    鼠标移到导航上面当前的LI变色处于当前的位置,广泛应用于当前导航。点击这里查看效果以下是源代码:1234鼠标移到导航上面当前的LI变色处于当前的位置-柯乐义51314152

    2021年12月20日
    56
  • GET请求方式的长度限制到底是多少?「建议收藏」

    GET请求方式的长度限制到底是多少?「建议收藏」在我的一贯认识中,一直认为get请求方式有长度限制,1024B。很抱歉在没有经过验证的情况下,一直奉为圭皋。直到项目中有一次用到get请求方式传值的时候,才发现之前一直记忆的网络知识一直都是错误的。今日,看到网络上关于get的知识总结,发现原来一直信奉的1024Get请求长度,是错误的。下面把从权威官网的解释复制过来,以做更正。1、Httpget方法提交的数据大小长度并没有限制,Http协议规范没有对URL长度进行限制。目前说的get长度有限制,是特定的浏览器及服务器

    2022年8月24日
    7
  • IDEA之配置SVN「建议收藏」

    IDEA之配置SVN「建议收藏」在实际公司开发的项目中,我们往往需要使用svn对特定功能模块进行版本管理,下面为IDEA配置SVN的相关步骤以及会遇到的一些问题目录一、SVN配置1.1下载SVN1.2安装SVN1.3配置SVN二、常见问题2.1.IDEA文件全部变红2.2Warning:java:源值1.5已过时2.2.1调整project版本一、SVN配置1.1下载SVNTortoiseSVN官网地址:https://tortoisesvn.net/downloads.html1.2安装SVN基本是

    2022年5月14日
    56
  • Laravel5性能优化技巧

    Laravel5性能优化技巧

    2022年3月8日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号