JSP中java脚本显示所有的书籍列表

*****************************************************
public class Book {
        private String id;
        private String name;
        private String author;
        private float  price;
        private String description;       
        public Book(){}
        public Book(String id, String name, String author, float price,
            String description) {
                super();
                this.id = id;
                this.name = name;
                this.author = author;
                this.price = price;
                this.description = description;
        }
        public String getId() {
                return id;
        }
      public void setId(String id) {
                this.id = id;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getAuthor() {
                return author;
        }
        public void setAuthor(String author) {
                this.author = author;
        }
        public float getPrice() {
                return price;
        }
        public void setPrice(float price) {
                this.price = price;
        }
        public String getDescription() {
                return description;
        }
        public void setDescription(String description) {
                this.description = description;
        }
        @Override
        public String toString() {
                return "Book [author=" + author + ", description=" + description
                    + ", id=" + id + ", name=" + name + ", price=" + price + "]";
        }   
}
*****************************************************
public class BookDB {

        // key: 书的id   value: id对应的书对象
        private static Map<String, Book> books = new LinkedHashMap<String, Book>();


        static{
                books.put("1", new Book("1", "葵花宝典", "葛付以", 5.00f, "欲练此功"));
                books.put("2", new Book("2", "玉女心经", "朱巧玲", 8.00f, "欲练此功"));
                books.put("3", new Book("3", "辟邪剑法", "邹海洋", 5.00f, "欲练此功"));
                books.put("4", new Book("4", "金瓶梅", "刘建平", 15.00f, "古代"));
                books.put("5", new Book("5", "红楼梦", "曹雪芹", 105.00f, "古代"));
        }
        
        public static Book findBookById(String bookId){
                return books.get(bookId);   // map集合的方法
        }
        
        public static Map<String,Book> findAllBooks(){
                return books;
        }

}
*****************************************************
public class ServletDemo1 extends HttpServlet {


        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                // 从数据库中查出所有书籍
                Map<String, Book> books = BookDB.findAllBooks();
                // 交给JSP去显示
                request.setAttribute("books", books);
                request.getRequestDispatcher("/10.jsp").forward(request, response);
        }


        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                doGet(request, response);
        }


}
*****************************************************
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cn.itcast.servlet.Book"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
        <head>         
              <title>书籍列表</title>       
              <meta http-equiv="pragma" content="no-cache">
              <meta http-equiv="cache-control" content="no-cache">
              <meta http-equiv="expires" content="0">    
              <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              <meta http-equiv="description" content="This is my page">
        </head>        
        <body>
                <h1>本站有以下好书</h1>
                <table border="1" width="88%">
                        <tr>
                                <th>书名</th>
                                <th>作者</th>
                                <th>售价</th>
                                <th>简介</th>
                        </tr>
                        <%
                        Map<String,Book> books = (Map<String,Book>)request.getAttribute("books"); 
                        for(Map.Entry<String,Book> me : books.entrySet()){
                                Book book = me.getValue();
                        %>
                              <tr>
                                      <td><%=book.getName() %></td>
                                      <td><%=book.getAuthor() %></td>
                                      <td><%=book.getPrice() %></td>
                                      <td><%=book.getDescription() %></td>
                              </tr>
                        <%
                        }
                        %>
                </table>
        </body>
</html>

*****************************************************








  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值