利用servlet实现对书籍书名、单价、数量等信息的添加,计算总价

1.题目要求

利用servlet实现对书籍书名、单价、数量等信息的添加,计算总价。

要求:输入两次表单信息,在一个成功返回的页面里面显示两次的数据。

利用servlet实现对书籍书名、单价、数量等信息的添加,计算总价_第1张图片

利用servlet实现对书籍书名、单价、数量等信息的添加,计算总价_第2张图片

2.Book实体类

package com.hjj.sevletgk.hw7.book;

/**
 * @author:嘉佳 Date:2023/10/8 15:16
 **/

public class Book {
    private double price;
    private int num;
    private String bookName;
    private double totalPrice;

    public Book(){

    }

    public Book(double price, int num, String bookName) {
        this.price = price;
        this.num = num;
        this.bookName = bookName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public double getTotalPrice() {
        return this.price*this.num;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

}

3.sevlet

package com.hjj.sevletgk.hw7.booksevlet;

import com.hjj.sevletgk.hw7.book.Book;

import javax.servlet.RequestDispatcher;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/book")
public class BookServlet extends HttpServlet {
    private List bookList=new ArrayList<>();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookName=req.getParameter("bookName");
        double price = Double.parseDouble(req.getParameter("price"));
        int num = Integer.parseInt(req.getParameter("num"));

        // 把录入的信息添加到列表中
        Book book = new Book(price,num,bookName);
        bookList.add(book);

        // 计算总价
        int totalNum=0;
        double totalPrice = 0;
        for (Book b : bookList) {
            totalNum += b.getNum();
            totalPrice += b.getTotalPrice();
        }

        req.setAttribute("bookList", bookList);
        req.setAttribute("totalPrice", totalPrice);
        req.setAttribute("totalNum", totalNum);

        // 转发到结果页面
        RequestDispatcher dispatcher = req.getRequestDispatcher("hw7/result.jsp");
        dispatcher.forward(req, resp);
    }
}

4.jsp

(1) order.jsp
<%--
  Created by IntelliJ IDEA.
  User: ALASIJIA
  Date: 2023/10/8
  Time: 15:11
  To change this template use File | Settings | File Templates.
--%>
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>


    
    信息录入


请输入购书信息




(2) result.jsp 
<%--
  Created by IntelliJ IDEA.
  User: ALASIJIA
  Date: 2023/10/8
  Time: 15:09
  To change this template use File | Settings | File Templates.
--%>
<%@page contentType="text/html;charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>
<%@page import="com.hjj.sevletgk.hw7.book.Book" %>
<%@ page import="java.util.List" %>


    信息查看
    
    


<%--
id:指定实例化的 JavaBean 对象的名称
class:指定要实例化的 JavaBean 对象的类的全类名
--%>


商品总价

<% request.setCharacterEncoding("UTF-8"); %> <% for (Book book : (List) request.getAttribute("bookList")) { %> <% } %> <%-- 该单元格要横跨 2 列--%>
书名 价格 数量 总价
<%= book.getBookName() %> <%= book.getPrice() %> <%= book.getNum() %> <%= book.getTotalPrice() %>
总计: 商品总数:<%= request.getAttribute("totalNum") %> 总价:<%= request.getAttribute("totalPrice") %>

你可能感兴趣的:(servlet,java,开发语言)