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


    1.题目要求

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

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

    2.Book实体类

    1. package com.hjj.sevletgk.hw7.book;
    2. /**
    3. * @author:嘉佳 Date:2023/10/8 15:16
    4. **/
    5. public class Book {
    6. private double price;
    7. private int num;
    8. private String bookName;
    9. private double totalPrice;
    10. public Book(){
    11. }
    12. public Book(double price, int num, String bookName) {
    13. this.price = price;
    14. this.num = num;
    15. this.bookName = bookName;
    16. }
    17. public double getPrice() {
    18. return price;
    19. }
    20. public void setPrice(double price) {
    21. this.price = price;
    22. }
    23. public int getNum() {
    24. return num;
    25. }
    26. public void setNum(int num) {
    27. this.num = num;
    28. }
    29. public String getBookName() {
    30. return bookName;
    31. }
    32. public void setBookName(String bookName) {
    33. this.bookName = bookName;
    34. }
    35. public double getTotalPrice() {
    36. return this.price*this.num;
    37. }
    38. public void setTotalPrice(double totalPrice) {
    39. this.totalPrice = totalPrice;
    40. }
    41. }

    3.sevlet

    1. package com.hjj.sevletgk.hw7.booksevlet;
    2. import com.hjj.sevletgk.hw7.book.Book;
    3. import javax.servlet.RequestDispatcher;
    4. import javax.servlet.ServletException;
    5. import javax.servlet.annotation.WebServlet;
    6. import javax.servlet.http.HttpServlet;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. import java.util.ArrayList;
    11. import java.util.List;
    12. @WebServlet("/book")
    13. public class BookServlet extends HttpServlet {
    14. private List bookList=new ArrayList<>();
    15. @Override
    16. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    17. String bookName=req.getParameter("bookName");
    18. double price = Double.parseDouble(req.getParameter("price"));
    19. int num = Integer.parseInt(req.getParameter("num"));
    20. // 把录入的信息添加到列表中
    21. Book book = new Book(price,num,bookName);
    22. bookList.add(book);
    23. // 计算总价
    24. int totalNum=0;
    25. double totalPrice = 0;
    26. for (Book b : bookList) {
    27. totalNum += b.getNum();
    28. totalPrice += b.getTotalPrice();
    29. }
    30. req.setAttribute("bookList", bookList);
    31. req.setAttribute("totalPrice", totalPrice);
    32. req.setAttribute("totalNum", totalNum);
    33. // 转发到结果页面
    34. RequestDispatcher dispatcher = req.getRequestDispatcher("hw7/result.jsp");
    35. dispatcher.forward(req, resp);
    36. }
    37. }

    4.jsp

    (1) order.jsp
    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: ALASIJIA
    4. Date: 2023/10/8
    5. Time: 15:11
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@page contentType="text/html;charset=UTF-8"%>
    9. <%@page pageEncoding="UTF-8"%>
    10. <html>
    11. <head>
    12. <meta charset="UTF-8">
    13. <title>信息录入</title>
    14. </head>
    15. <body>
    16. <h2>请输入购书信息</h2>
    17. <form method="post" action="${pageContext.request.contextPath}/book" >
    18. <label for="bookName">书名</label>
    19. <input type="text" name="bookName" id="bookName" required><br/>
    20. <label for="price">单价</label>
    21. <input type="text" name="price" id="price" required><br/>
    22. <label for="num">数量</label>
    23. <input type="text" name="num" id="num" required><br/>
    24. <input type="submit" value="提交"/>
    25. </form>
    26. </body>
    27. </html>
    (2) result.jsp 
    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: ALASIJIA
    4. Date: 2023/10/8
    5. Time: 15:09
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@page contentType="text/html;charset=UTF-8" %>
    9. <%@page pageEncoding="UTF-8" %>
    10. <%@page import="com.hjj.sevletgk.hw7.book.Book" %>
    11. <%@ page import="java.util.List" %>
    12. <html>
    13. <head>
    14. <title>信息查看</title>
    15. <meta charset="UTF-8">
    16. <style>
    17. h2 {
    18. text-align: center;
    19. }
    20. table {
    21. /* 合并边框 */
    22. border-collapse: collapse;
    23. height: 80px;
    24. /* 居中 */
    25. margin: 0 auto;
    26. }
    27. th {
    28. /* 内边距 */
    29. padding: 5px 20px;
    30. }
    31. table, th, td {
    32. border: 1px solid #000;
    33. }
    34. </style>
    35. </head>
    36. <body>
    37. <%--
    38. id:指定实例化的 JavaBean 对象的名称
    39. class:指定要实例化的 JavaBean 对象的类的全类名
    40. --%>
    41. <jsp:useBean id="Book" class="com.hjj.sevletgk.hw7.book.Book"/>
    42. <jsp:setProperty name="Book" property="*"/>
    43. <h2>商品总价</h2>
    44. <%
    45. request.setCharacterEncoding("UTF-8");
    46. %>
    47. <table>
    48. <tr>
    49. <th>书名</th>
    50. <th>价格</th>
    51. <th>数量</th>
    52. <th>总价</th>
    53. </tr>
    54. <% for (Book book : (List<Book>) request.getAttribute("bookList")) { %>
    55. <tr>
    56. <td><%= book.getBookName() %></td>
    57. <td><%= book.getPrice() %></td>
    58. <td><%= book.getNum() %></td>
    59. <td><%= book.getTotalPrice() %></td>
    60. </tr>
    61. <% } %>
    62. <tr>
    63. <%-- 该单元格要横跨 2 列--%>
    64. <td colspan="2"><b>总计:</b></td>
    65. <td><b>商品总数:</b><%= request.getAttribute("totalNum") %></td>
    66. <td><b>总价:</b><%= request.getAttribute("totalPrice") %></td>
    67. </tr>
    68. </table>
    69. </body>
    70. </html>

  • 相关阅读:
    Vue.js 3.x 响应式系统原理
    翻译QT使用手册:将库添加到项目
    剑指 Offer 17. 打印从1到最大的n位数(大数问题)
    【排序算法】冒泡排序
    python中的GUI自动化工具介绍
    3.你所不知道的go语言控制语句——Leetcode习题69
    ARC147E Examination
    SpringCloud 微服务全栈体系(三)
    springboot+vue实现excel的导出
    java计算机毕业设计高校疫情管理源码+数据库+系统+lw文档+mybatis+运行部署
  • 原文地址:https://blog.csdn.net/GANTENJ/article/details/134301244