Dark Horse day05 Cookie small case & record the last accessed book, dark horse day05cookie
1. Create a javaBean Book
package cn.itheima.domain;import java.io.Serializable;public class Book implements Serializable{private String id;private String name;private String description;private String author;private String price;public Book(){}public Book(String id, String name, String description, String author,String price) {super();this.id = id;this.name = name;this.description = description;this.author = author;this.price = price;}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 getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}}
2. Create a BookDao simulation database
Package cn. itheima. dao; import java. util. linkedHashMap; import java. util. map; import cn. itheima. domain. book; public class BookDao {private static Map <String, Book> books = new LinkedHashMap <String, Book> (); // initialize static {books. put ("1", new Book ("1", "Water Margin", "The Story of 105 men and 3 women boarding Liangshan... "," Li Weikang "," 50.0 "); books. put ("2", new Book ("2", "Jin Pingmei", "unsightly stories... "," Jin Yong "," 10.2 "); books. put ("3", new Book ("3", "Journey to the West", "a monk, a monkey, a fat pig, and a bald man go to the West to learn the story... "," Han Wei "," 100.5 "); books. put ("4", new Book ("4", "Romance of the Three Kingdoms", "stories of war in the Three Kingdoms period... "," Weng zongshun "," 30.1 ");} public static Book getBook (String id) {return books. get (id) ;}public static Map <String, Book> getBooks () {return books ;}}
3. Create a BookListServlet for the book list
Package cn. itheima. cookie; import java. io. IOException; import java. util. map; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import cn. itheima. dao. bookDao; import cn. itheima. domain. book; public class BookListServlet extends HttpServlet {public void doGet (HttpServletRequest request, Http ServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = UTF-8"); // 1. map <String, Book> books = BookDao. getBooks (); for (Map. entry <String, Book> id: books. entrySet () {Book book = id. getValue (); response. getWriter (). write ("<a href = '" + request. getContextPath () + "/servlet/BookInfoServlet? Id = "+ book. getId () + "'>" + book. getName () + "</a> <br/>") ;}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
4. Create BookInfoServlet to display detailed information of books
Package cn. itheima. cookie; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. cookie; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import cn. itheima. dao. bookDao; import cn. itheima. domain. book; public class BookInfoServlet extends HttpServlet {public void doGet (HttpServletRequest r Equest, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = UTF-8"); // 1. obtain the idString id = request. getParameter ("id"); Book book = BookDao. getBook (id); // 2. show the book information response. getWriter (). write ("5. Running result: