package app05a;
/**
* 书籍对象
* @author Administrator
*
*/
public class Book {
private String isbn; // 码
private String title; // 主题
private double price; // 价格
public Book(String isbn, String title, double price) {
super();
this.isbn = isbn;
this.title = title;
this.price = price;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package app05a;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;
/**
* 书籍业务控制类(init --> service --> doget --> dopost)
*/
@WebServlet( name = "BooksServlet", urlPatterns = "/books")
public class BooksServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BooksServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* doget做业务处理并重定向
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建对象集合,便于foreach遍历
List<Book> books = new ArrayList<Book>();
Book book1 = new Book("978-0980839616",
"Java 7: A Beginner's Tutorial", 45.00);
Book book2 = new Book("978-0980331608",
"Struts 2 Design and Programming: A Tutorial",
49.95);
Book book3 = new Book("978-0975212820",
"Dimensional Data Warehousing with MySQL: A "
+ "Tutorial", 39.95);
//添加入集合
books.add(book1);
books.add(book2);
books.add(book3);
//向页面传递参数
request.setAttribute("books", books);
//重定向
RequestDispatcher rd = request.getRequestDispatcher("/BookList.jsp");
rd.forward(request, response);
}
}
jsp页面展示:(需要引入两个jar包,可以百度搜索)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
table,tr,td{
border: 1px solid brown;
}
</style>
</head>
<body>
Books in Simple Table
<table>
<tr>
<td>ISBN</td>
<td>Title</td>
</tr>
<!-- for each 遍历 -->
<c:forEach items = "${ requestScope.books}" var = "book">
<tr>
<td>${ book.isbn}</td>
<td>${book.title}</td>
</tr>
</c:forEach>
</table>
<br/>
Books in Styled Table
<table>
<tr style="background: #ababff;">
<td>ISBN</td>
<td>Title</td>
</tr>
<!-- for each遍历 -->
<c:forEach items = "${ requestScope.books}" var="book" varStatus="status">
<!-- if 判断换行变色 -->
<c:if test="${ status.count%2==0}">
<tr style="background:#eef">
</c:if>
<c:if test="${ status.count%2 != 0}">
<tr style="background:#dedeff">
</c:if>
<td>${book.isbn}</td>
<td>${book.title}</td>
</tr><!-- 这个tr对应c:if中的tr -->
</c:forEach>
</table>
<br/>
ISBNs only:
<c:forEach items = "${ requestScope.books}" var="book" varStatus="status">
${book.isbn }<c:if test="${!status.last}">,</c:if> <!-- 不是最后一个要加逗号 -->
</c:forEach>
</body>
</html>