做一个网站需要到哪里做,钟表企业网站管理系统,数据查询网站建设,十大软件下载软件大全1.题目要求
利用servlet实现对书籍书名、单价、数量等信息的添加#xff0c;计算总价。
要求#xff1a;输入两次表单信息#xff0c;在一个成功返回的页面里面显示两次的数据。 2.Book实体类
package com.hjj.sevletgk.hw7.book;/*** author:嘉佳 Date:2023/10/8 15:16*…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 ListBook bookListnew ArrayList();Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String bookNamereq.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 totalNum0;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 contentTypetext/html;charsetUTF-8%
%page pageEncodingUTF-8%
html
headmeta charsetUTF-8title信息录入/title
/head
body
h2请输入购书信息/h2
form methodpost action${pageContext.request.contextPath}/book label forbookName书名/labelinput typetext namebookName idbookName requiredbr/label forprice单价/labelinput typetext nameprice idprice requiredbr/label fornum数量/labelinput typetext namenum idnum requiredbr/input typesubmit 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 contentTypetext/html;charsetUTF-8 %
%page pageEncodingUTF-8 %
%page importcom.hjj.sevletgk.hw7.book.Book %
% page importjava.util.List %
html
headtitle信息查看/titlemeta charsetUTF-8styleh2 {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 idBook classcom.hjj.sevletgk.hw7.book.Book/
jsp:setProperty nameBook property*/
h2商品总价/h2
%request.setCharacterEncoding(UTF-8);
%tabletrth书名/thth价格/thth数量/thth总价/th/tr% for (Book book : (ListBook) request.getAttribute(bookList)) { %trtd% book.getBookName() %/tdtd% book.getPrice() %/tdtd% book.getNum() %/tdtd% book.getTotalPrice() %/td/tr% } %tr%-- 该单元格要横跨 2 列--%td colspan2b总计:/b/tdtdb商品总数:/b% request.getAttribute(totalNum) %/tdtdb总价:/b% request.getAttribute(totalPrice) %/td/tr/table
/body
/html