*****************************************************
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>
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>
*****************************************************