JSP中java脚本显示所有的书籍列表
2015-09-10 11:24
387 查看
*****************************************************
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>
*****************************************************
相关文章推荐
- Java与时间相关的类
- java字符串反转方法总结
- MyEclipse注册码,有效期至2017年
- spring service方法里有两个save时的同步问题
- Java正则表达式Pattern和Matcher详解
- java常见内存溢出(OOM)解决方案
- eclipse安装spring插件
- MyEclipse8.5中属性设置集锦(不定时更新)
- ubuntu java环境变量配置
- Java数据类型
- eclipse res里面的资源文件命名问题
- 使用Eclipse的wsdl2java工具
- myeclipse经常弹出Subversion Native Library Not Available框解决办法
- Java中对浮点数精度的控制
- 重拾java系列一java基础(1)
- Eclipse启动时卡死解决方法
- Thinking in java 之:容器介绍
- Mahout--最基本的推荐系统的JAVA代码
- Java中equals和==的区别
- Zookeeper Api(java)入门与应用(转)