0


Java Web——简易购物车网页设计

实验三** JSP**内置对象使用

一、实验目的

1.熟悉request、response、session、application、out等内置对象。

2.理解购物车概念。

二、实验学时

2H

三、实验性质

   综合性实验

四、实验内容

开发一个简易购物车,要求如下:

1.编写两个页面,一个显示一些历史图书的名称和价格,一个显示一些计算机图书的名称和价格。在每本书的后面都有一个链接——购买,单击链接,能够将该书添加到购物车。(45分)

2.在每个页面上都有链接“显示购物车”,单击该链接,能够显示购物车中的内容;在每个内容后面都有一个“删除”链接,单击链接,可以将该图书从购物车中删除。(45分)

历史图书的名称和价格(代码):

<%@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>历史图书</title>
</head>
<body>
    <%
        Map<String, Integer> historyBooks = new HashMap<>();
        historyBooks.put("史记", 86);
        historyBooks.put("人类简史", 46);
        historyBooks.put("西游记", 66);
        historyBooks.put("红楼梦", 35);
        historyBooks.put("水浒传", 55);
    %>
    <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>

历史图书的名称和价格(运行结果截图):

计算机图书的名称和价格(代码):

<%@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>计算机图书</title>
</head>
<body>
    <%
        Map<String, Integer> computerBooks = new HashMap<>();
        computerBooks.put("Java程序设计", 86);
        computerBooks.put("Java高级编程", 46);
        computerBooks.put("数据库系统与实现", 66);
        computerBooks.put("设计模式", 35);
        computerBooks.put("计算机网络", 55);
    %>
    <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>

计算机图书的名称和价格(运行结果截图):

(添加) 购物车代码:

<%@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>添加购物车</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);
            }  
                cart.put(name, Integer.parseInt(price));
            }
        
        if ("history".equals(type)) {
            response.sendRedirect("history_book.jsp");
        } else {
            response.sendRedirect("computer_book.jsp");
        }
        Map<String, Integer> cart = new HashMap<>();
    %>
</body>
</html>

(添加)购物车(运行结果截图):

** (查看) 购物车代码:**

<%@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>查看购物车</title>
</head>
<body>
    <%
        Map<String, Integer> cart = (Map<String, Integer>) session.getAttribute("cart");
        if (cart != null && cart.size() > 0) {
    %>
    <h2>查看购物车</h2>
    <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;
                    //Integer num;
                    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>

(查看)购物车(运行结果截图):

** (删除) 购物车代码:**

<%@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>删除购物车</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>

(删除水浒传后)购物车(运行结果截图):


本文转载自: https://blog.csdn.net/qq_67204070/article/details/131753983
版权归原作者 ꪖ᭙ꪗ。 所有, 如有侵权,请联系我们删除。

“Java Web——简易购物车网页设计”的评论:

还没有评论