当前位置: 首页 > news >正文

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

1.题目要求

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

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

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<Book> bookList=new ArrayList<>();@Overrideprotected 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: ALASIJIADate: 2023/10/8Time: 15:11To change this template use File | Settings | File Templates.
--%>
<%@page contentType="text/html;charset=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head><meta charset="UTF-8"><title>信息录入</title>
</head>
<body>
<h2>请输入购书信息</h2>
<form method="post" action="${pageContext.request.contextPath}/book" ><label for="bookName">书名</label><input type="text" name="bookName" id="bookName"  required><br/><label for="price">单价</label><input type="text" name="price" id="price" required><br/><label for="num">数量</label><input type="text" name="num" id="num" required><br/><input type="submit" value="提交"/>
</form>
</body>
</html>
(2) result.jsp 
<%--Created by IntelliJ IDEA.User: ALASIJIADate: 2023/10/8Time: 15:09To 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" %>
<html>
<head><title>信息查看</title><meta charset="UTF-8"><style>h2 {text-align: center;}table {/* 合并边框 */border-collapse: collapse;height: 80px;/* 居中 */margin: 0 auto;}th {/* 内边距 */padding: 5px 20px;}table, th, td {border: 1px solid #000;}</style>
</head>
<body>
<%--
id:指定实例化的 JavaBean 对象的名称
class:指定要实例化的 JavaBean 对象的类的全类名
--%>
<jsp:useBean id="Book" class="com.hjj.sevletgk.hw7.book.Book"/>
<jsp:setProperty name="Book" property="*"/>
<h2>商品总价</h2>
<%request.setCharacterEncoding("UTF-8");
%><table><tr><th>书名</th><th>价格</th><th>数量</th><th>总价</th></tr><% for (Book book : (List<Book>) request.getAttribute("bookList")) { %><tr><td><%= book.getBookName() %></td><td><%= book.getPrice() %></td><td><%= book.getNum() %></td><td><%= book.getTotalPrice() %></td></tr><% } %><tr><%--        该单元格要横跨 2 列--%><td colspan="2"><b>总计:</b></td><td><b>商品总数:</b><%= request.getAttribute("totalNum") %></td><td><b>总价:</b><%= request.getAttribute("totalPrice") %></td></tr></table>
</body>
</html>

http://www.lryc.cn/news/225668.html

相关文章:

  • 一键批量转码:将MP4视频转为MP3音频的简单方法
  • java入门,记一次微服务间feigin请求的问题
  • HarmonyOS应用开发者高级认证(88分答案)
  • 离散Hopfield神经网络分类——高校科研能力评价
  • Run highlighted commands using IDE
  • vscode文件跳转(vue项目)
  • 嵌入式Linux系统中内存分配详解
  • 4、FFmpeg命令行操作4
  • 如何通过命令查看某一文件的内容改动和提交记录
  • 更安全的ssh协议与Gui图形化界面使用
  • ❤ Uniapp使用 ( 三 配置和各种使用篇)
  • k8s 创建普通用户使用
  • 【微软技术栈】C#.NET 依赖项注入
  • 评国青、优青、杰青,到底需要什么级别的文章?五篇代表作如何选?
  • 使用双动态令牌混合器学习全局和局部动态以进行视觉识别
  • Flutter笔记 - 关于 fit 属性以及相关知识的总结
  • 如何在PPT中去除编辑密码?
  • Kotlin库实现多线程爬取数据
  • RT-Thread Env使用
  • 2011年09月21日 Go生态洞察:Go图像处理包
  • 《QT从基础到进阶·十七》QCursor鼠标的不同位置坐标获取
  • K8s----资源管理
  • java.net.UnknownServiceException: CLEARTEXT communication to 127.0.0.1 not p
  • STM32——系统时钟(概述,问题总结)
  • 魔众文库系统 v5.5.0 批量快捷上传,文档图标优化,档转换逻辑优化
  • 52. 携带研究材料
  • 局域网内部服务器访问外部网络
  • IP行业API助力于网络分析和数据挖掘
  • Azure 机器学习 - 如何使用模板创建安全工作区
  • 可变类与不可变类