该案例提供了两类商品,水果和肉类,不同的物品需要在不同的页面浏览,并添加到购物车里,最后在购物车里显示购买的商品。
meat.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>肉类页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
if(request.getParameter("c1")!=null){
session.setAttribute("s1",request.getParameter("c1") );
}
if(request.getParameter("c2")!=null){
session.setAttribute("s2",request.getParameter("c2"));
}
if (request.getParameter("c3")!=null){
session.setAttribute("s3",request.getParameter("c3"));
}
%>
肉类大甩卖,跳楼价!!!
<hr/>
<form action="meat.jsp" method="post">
猪肉<input type="checkbox" name="c1" value="猪肉"/>
牛肉<input type="checkbox" name="c2" value="牛肉"/>
羊肉<input type="checkbox" name="c3" value="羊肉"/>
<p>
<input type="submit" value="提交" name="c4"/><br/>
<a href="fruit.jsp"/>买点其他的<br/>
<a href="shoppingCar.jsp"/>查看购物车<br>
</form>
</body>
</html>
fruit.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>水果页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
if(request.getParameter("b1")!=null){
session.setAttribute("s4",request.getParameter("b1"));
}
if (request.getParameter("b2")!=null){
session.setAttribute("s5",request.getParameter("b2"));
}
if(request.getParameter("b3")!=null){
session.setAttribute("s6",request.getParameter("b3"));
}
%>
水果大甩卖!!!不甜不要钱
<form action="fruit.jsp" method="post">
苹果<input type="checkbox" name="b1" value="苹果"/>
香蕉<input type="checkbox" name="b2" value="香蕉"/>
榴莲<input type="checkbox" name="b3" value="榴莲"/>
<input type="submit" value="提交" name="b4"/><br>
<a href="meat.jsp"/>买点肉
<a href="shoppingCar.jsp"/>查看购物车
</form>
</body>
</html>
shoppingCar.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>购物车页面</title>
</head>
<body>
这是你选择的商品:
<%
request.setCharacterEncoding("UTF-8");
String str ="";
if(session.getAttribute("s1")!=null){
str = (String)session.getAttribute("s1");
out.println(str+"<br>");
}
if(session.getAttribute("s2")!=null){
str = (String)session.getAttribute("s2");
out.println(str+"<br>");
}
if(session.getAttribute("s3")!=null){
str = (String)session.getAttribute("s3");
out.println(str+"<br>");
}
if(session.getAttribute("s4")!=null){
str = (String)session.getAttribute("s4");
out.println(str+"<br>");
}
if(session.getAttribute("s5")!=null){
str = (String)session.getAttribute("s5");
out.println(str+"<br>");
}
if(session.getAttribute("s6")!=null){
str = (String)session.getAttribute("s6");
out.println(str+"<br>");
}
%>
</body>
</html>