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>