内置对象的使用

add_cast.jsp

<%@ page import="java.util.*" %>
<%@ 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>
<%
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String type = request.getParameter("type");
        if(name!=null && price!=null) {
            Map<String,Integer> cart= (Map<String,Integer>)session.getAttribute("cart");
            if(cart ==null){
                cart= new HashMap<>();
                session.setAttribute("cart",cart);
            }else {
                cart.put(name,Integer.parseInt(price));
            }
        }
        if("history".equals(type)){
            response.sendRedirect("history_book.jsp");
        }else {
            response.sendRedirect("computer_book.jsp");
        }

    %>
</body>
</html>

computer_book.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.HashMap" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>计算机图书</title>
</head>
<body>
<%
    Map<String,Integer> computerBooks  =new HashMap<>();
    computerBooks.put("JAVA程序设计",86);
    computerBooks.put("Java高级编程",46);
    computerBooks.put("数据库系统与实现",96);
    computerBooks.put("设计模式",76);
    computerBooks.put("计算机网络",66);
%>
<h2>计算机图书列表</h2>
<table border="1">
    <thead>
    <tr>
        <td>名称<td>
        <td>价格</td>
        <td>功能</td>
    </tr>
    </thead>
    <tbody>
    <%
        Set<String> keySet = computerBooks.keySet();
        Iterator<String> it  = keySet.iterator();
        String key;
        Integer value;
        while(it.hasNext()){
            key = it.next();
            value = computerBooks.get(key);
    %>
    <tr>
        <td><%=key%><td>
        <td><%=value%></td>
        <td><a href="add_cart.jsp?name=<%=key%>&price=<%=value%>&type=computer">购买</a></td>
    </tr>
    <%
        }
    %>
    </tbody>
</table>
    <div><a href="show_cart.jsp">查看购物车</a></div>
    <div><a href="history_book.jsp">历史图书列表</a></div>

</body>
</html>

delete_cart.jsp

<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>删除购物车</title>
</head>
<body>
    <%
      Map<String,Integer> cart= (Map<String,Integer>) session.getAttribute("cart");
      String  name= request.getParameter("name");
      if(name!=null){
        cart.remove(name);
      }
      response.sendRedirect("show_cart.jsp");
    %>

</body>
</html>

histrot_book.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.HashMap" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>历史图书</title>
</head>
<body>
    <%
        Map<String,Integer> historyBooks  =new HashMap<>();
        historyBooks.put("史记",86);
        historyBooks.put("人类简史",46);
        historyBooks.put("西游记",96);
        historyBooks.put("红楼梦",76);
        historyBooks.put("水浒传",66);
    %>
    <h2>历史图书列表</h2>
    <table border="1">
        <thead>
        <tr>
            <td>名称<td>
            <td>价格</td>
            <td>功能</td>
        </tr>
        </thead>
        <tbody>
        <%
            Set<String> keySet = historyBooks.keySet();
            Iterator<String> it  = keySet.iterator();
            String key;
            Integer value;
            while(it.hasNext()){
                key = it.next();
                value = historyBooks.get(key);
        %>
        <tr>
            <td><%=key%><td>
            <td><%=value%></td>
            <td><a href="add_cart.jsp?name=<%=key%>&price=<%=value%>&type=history">购买</a></td>
        </tr>
        <%
            }
        %>
        </tbody>
    </table>
    <div><a href="show_cart.jsp">查看购物车</a></div>
    <div><a href="computer_book.jsp">计算机图书列表</a></div>

</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>购物商城</title>
</head>
<body>
    <a href="history_book.jsp"> 历史图书</a>
    <a href="computer_book.jsp"> 计算机图书</a>
</body>
</html>

show_cart.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.HashMap" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>查看购物车</title>
</head>
<body>
<h2>查看购物车</h2>
        <%
        Map<String,Integer> cart= (Map<String,Integer>) session.getAttribute("cart");
        if(cart!=null && cart.size()>0){
        %>
        <table border="1">
                <thead>
                <tr>
                        <td>名称<td>
                        <td>价格</td>
                        <td>功能</td>
                </tr>
                </thead>
                <tbody>
                <%
                        Set<String> keySet = cart.keySet();
                        Iterator<String> it  = keySet.iterator();
                        String name;
                        Integer price;
                        while(it.hasNext()){
                                name = it.next();
                                price = cart.get(name);
                %>
                <tr>
                        <td><%=name%><td>
                        <td><%=price%></td>
                        <td><a href="delete_cart.jsp?name=<%=name%>">删除</a></td>
                </tr>
                <%
                        }
                %>
                </tbody>
        </table>
        <%
        }else{
                out.print("购物车空空如也");
        }

        %>
        <div><a href="history_book.jsp">历史图书列表</a></div>
        <div><a href="computer_book.jsp">计算机图书列表</a></div>
</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值