平台:eclipse.javaee
题目要求:
用cookie实现购物车功能,购物车已存在该商品则不重复添加,不存在则添加进去,最后打印出购物车中的商品。
1.xg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品选购</title>
</head>
<body>
<center>
<img src="西游记.jpg" alt="西游记"width="300" high="320"/><br>
<a href="xyj.jsp">《西游记》</a> 罗贯中著,正版。 49¥ <br>
<img src="海尔.jpg" alt="海尔热水器"width="300" high="300"/><br>
<a href="rsq.jsp">热水器</a>海尔正品保证,十分钟速热。 1999¥<br>
<img src="奥克斯.jpg" alt="奥克斯空调"width="300" high="300"/><br>
<a href="kt.jsp">空调</a>奥克斯空调,三十年品质保障,让你买的实惠用得放心。 2999¥<br>
<a href="purchase.jsp">查看购物车</a>
</center>
</body>
</html>
2.xyj.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>西游记</title>
</head>
<body>
<center>
<h1>商品名称:《西游记》</h1>
<img src="西游记.jpg" alt="西游记"width="300" high="320"/><br><br>
《西游记》 49¥ 正版发售。人民出版社<br>
<a href="shoppingservlet?id=0">加入购物车</a>
<a href="xg.jsp">返回上一页</a><br>
<br><br>
简介:<br>
《西游记》是中国古典四大名著之一,由吴承恩编写而成。<br>
此书描写的是孙悟空、猪八戒、沙悟净保护唐僧西天取经的传奇故事。 <br>
作者吴承恩运用浪漫主义手法,翱翔着无比丰富的想象的翅膀,描绘了一个色彩缤纷、<br>
神奇瑰丽的幻想世界,创造了一系列妙趣横生、引人入胜的神话故事,<br>
成功地塑造了孙悟空这个超凡入圣的理想化的英雄形象。<br>
</center>
</body>
</html>
3.rsq.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<title>海尔热水器</title>
</head>
<body>
<center>
<h1>商品名称:海尔热水器</h1>
<img src="海尔.jpg" alt="海尔热水器"width="300" high="320"/><br><br>
海尔热水器 参考价:1999¥ 正品保证。16升水气双调恒温燃气热水器<br>
颜色: 轻奢金<br>
容积:16升 能效等级:2级<br>
气源:天然气,水气双调 节能省气<br>
WiFi智能 五重抑菌<br>
<a href="shoppingservlet?id=1">加入购物车</a>
<a href="xg.jsp">返回上一页</a><br>
<br><br>
</center>
</body>
</html>
4.kt.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>奥克斯空调</title>
</head>
<body>
<center>
<h1>商品名称:奥克斯空调</h1>
<img src="奥克斯.jpg" alt="奥克斯空调"width="300" high="320"/><br><br>
奥克斯空调 参考价:2999¥ 正品保证。奥克斯 KFR-35GW/BpSFA-3 空调<br>
主要参数:<br>
空调类型:挂式空调 产品功率:大1.5P<br>
变频:是 冷暖类型:冷暖型<br>
能效级别:3级 控制方式:遥控<br>
<a href="shoppingservlet?id=2">加入购物车</a>
<br>
<a href="xg.jsp">返回上一页</a><br>
<br><br>
</center>
</body>
</html>
5.tz.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta http-equiv="refresh" content="3;URL=http://localhost:8080/shopping/xg.jsp">
<%/*3秒之后自动跳转到res.html,两个属于同一文件下面,要是需要跳转到jsp页面,
就需要在url里面填写url地址————(浏览器的地址栏里面写入的数据,如:http://localhost:8080/TestDemo/1.jsp)
*/%>
</head>
<body>
<h2 align="center">加入购物车成功!</h2><br>
<h3 align="center">三秒后自动跳转.......</h3>
</body>
</html>
6.cu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta http-equiv="refresh" content="3;URL=http://localhost:8080/shopping/xg.jsp">
</head>
<body>
<h2 align="center">购物车中已存在!</h2><br>
<h3 align="center">三秒后自动跳转.......</h3>
</body>
</html>
7.purchase.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<center>
<h1>欢迎亲亲查看购物车</h1>
<%String[] products = {"西游记","海尔热水器","奥克斯空调","华为荣耀8X全面屏手机","java SE基础教程"};
//得到所有的cookie
Cookie[] cookies = request.getCookies();
//对cookies进行遍历
if(cookies!=null){
response.getWriter().println("亲亲,您的购物车清单如下:<br/>");
for(Cookie co:cookies){
for(int i=0;i<products.length;i++){
if(!co.getName().equals("product"+i)){
continue;
}else{
response.getWriter().println("商品:"+products[i]+"\t 数量:"+co.getValue()+"<br/>");
}
}
}
} %>
<a href="xg.jsp">返回</a>
</center>
</body>
</html>
8.shoppingservlet
package shopping;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
/**
* Servlet implementation class addCarServlet
*/
public class shoppingservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public shoppingservlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("textml;charset=utf-8");
int count = 0;
Cookie cookie = new Cookie("cookie","a");
cookie.setMaxAge(60);
response.addCookie(cookie); //将session作为新的cookie添加到浏览器
//拿到用户本次点击的商品标识id
String id = request.getParameter("id");
String value = null;
//定义一个商品数组
String[] products = {"西游记","海尔热水器","奥克斯空调","华为荣耀8X全面屏手机","java SE基础教程"};
Cookie[] cookies1 = request.getCookies(); //得到所有的cookie
if(cookies1!=null){
for(Cookie co:cookies1){
//对cookies1进行遍历,如果有名为改书籍的cookie就拿出来
if(co.getName().equals("product"+(Integer.parseInt(id)))){
value = co.getValue(); //这个值应该是书籍的数量
//判断拿出来的value是不是为空---不管他是不是空的,我都要给他加1个
count = Integer.parseInt(value);
if(count>=0) {
//response.setHeader("location","cu.jsp");
response.sendRedirect("cu.jsp");
return;
}
}
else
{
count=1;
//因为第一次点击他是绝对不会存在的,所以...
}
}
}
Cookie cookie1 = new Cookie("product"+(Integer.parseInt(id)),""+count);
response.addCookie(cookie1);
response.sendRedirect("tz.jsp");
//并且将新的值传回浏览器
// Cookie cookie1 = new Cookie("product"+(Integer.parseInt(id)),""+count);
// response.addCookie(cookie1);
//
// response.sendRedirect("tz.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}