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.descri ption = 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. Establish 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> ();//Initialization static{ Books.put ("1", New book ("1", "Outlaws of the Marsh", "105 men and 3 women boarded the Liangshan story ...", "Li Weikang", "50.0")), Books.put ("2", New book ("2", "Golden Bottle plum", " Unsightly story ... "," Jin Yong "," 10.2 "); Books.put (" 3 ", New book (" 3 "," Journey to the West "," a monk, a monkey, a fat pig and a bald-headed to the west of the west to learn the story ... "," Anlu "," 100.5 ")); Books.put ("4", New book ("4", "Kingdoms", "The Story of war in the Three Kingdoms period ...", "Onzongshun", "30.1")); public static book GetBook (String ID) {return books.get (ID);} public static map<string,book> Getbooks () {return books;}}
3. Create a list of books Booklistservlet
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, httpservletresponse response) throws Servletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8");//1. Querying information from the database to display books 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. Set up Bookinfoservlet to display the detailed information of the book
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 request, httpservletresponse response) Throws Servletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8");//1. Get the idstring ID of the book = Request.getparameter ("id"); Book books = Bookdao.getbook (ID);//2. Displays the information Response.getwriter (). Write ("5. Operation Result:
Dark Horse day05 Cookie small case & record last visited book