代码如下:

Shopping.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Shopping.view")
public class Shopping extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String bookID = request.getParameter("bookID");
        HttpSession session = request.getSession();
        List<Book> bookList = (List<Book>) session.getAttribute("bookList");

        if (bookList == null) {
            bookList = new ArrayList<Book>();
            session.setAttribute("bookList", bookList);
        }

        boolean found = false;
        for (Book book : bookList) {
            if (book.getBookID().equals(bookID)) {
                book.setQuantity(book.getQuantity() + 1);
                found = true;
                break;
            }
        }

        if (!found) {
            Book book = new Book(bookID, getBookTitle(bookID), 1);
            bookList.add(book);
        }

        response.sendRedirect(response.encodeRedirectURL("Shopping.jsp"));
    }

    private String getBookTitle(String bookID) {
        // 根据bookID查询数据库,获取书名
        return "书名";
    }
}

Book.java

public class Book {
    private String bookID;
    private String title;
    private int quantity;

    public Book(String bookID, String title, int quantity) {
        this.bookID = bookID;
        this.title = title;
        this.quantity = quantity;
    }

    public String getBookID() {
        return bookID;
    }

    public void setBookID(String bookID) {
        this.bookID = bookID;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

Shopping.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
    <h1>购物车</h1>

    <table border="1">
        <tr>
            <th>图书ID</th>
            <th>书名</th>
            <th>数量</th>
        </tr>
        <c:forEach var="book" items="${bookList}">
            <tr>
                <td>${book.bookID}</td>
                <td>${book.title}</td>
                <td>${book.quantity}</td>
            </tr>
        </c:forEach>
    </table>

    <br>
    <a href="Shopping.view?bookID=0001">购买书籍1</a>
</body>
</html>

注:这里使用了JSTL标签库,需要在web.xml中配置:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <el-ignored>false</el-ignored>
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>
请使用中文并用java web编写以下题目并给出完整代码实现一个购物车应用程序可以购物显示购物数量并查看购物车内容。提示:采购此书做成超链接并用URL重写带关键信息a	ShoppingviewbookID=‘0001’其中0001是你选中图书对应的ID号每点击一次就在相应的购书数量上加一并返回当前的选购页面。b	除了要保存图书的ID号、书名还要保存购书数量所以session对象保存的信息需要考虑清

原文地址: https://www.cveoy.top/t/topic/bfgF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录