您的位置:首页 > 其它

黑马day05 Cookie小案例&记录上次访问的图书

2015-06-19 13:03 399 查看
1.建立一个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.建立BookDao模拟数据库

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>();
	//进行初始化
	static{
		books.put("1", new Book("1", "水浒传", "105个爷们和3个女人登上梁山的故事...", "李卫康", "50.0"));
		books.put("2", new Book("2", "金瓶梅", "不堪入目的故事...", "金庸", "10.2"));
		books.put("3", new Book("3", "西游记", "一个和尚,一个猴子,一个肥猪和一个秃子去西天取经的故事...", "韩玮", "100.5"));
		books.put("4", new Book("4", "三国演义", "三国时期打仗的故事...", "翁宗顺", "30.1"));
	}
	public static Book getBook(String id){
		return books.get(id);
	}
	public static Map<String,Book> getBooks(){
		return books;
	}
}
3.建立图书列表的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.从数据库中查询显示图书的信息
		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.建立BookInfoServlet进行图书的详细信息显示

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.得到书的id
		String id = request.getParameter("id");
		Book book = BookDao.getBook(id);
		//2.显示书的信息
		response.getWriter().write("<h3>"+"书名:"+book.getName()+"<h3/>");
		response.getWriter().write("<h3>"+"作者:"+book.getAuthor()+"<h3/>");
		response.getWriter().write("<h3>"+"售价:"+book.getPrice()+"<h3/>");
		response.getWriter().write("<h3>"+"描述:"+book.getDescription()+"<h3/>");
		
		//上次浏览的书籍
		Cookie[] cookies = request.getCookies();
		Cookie findC=null;
		if(cookies!=null){
			for(Cookie c:cookies){
				findC=c;
			}
		}
		if(findC==null){
			response.getWriter().write("<hr/>没有浏览记录!");
		}else{
			String value = findC.getValue();
			response.getWriter().write("<hr/>您上次浏览的书籍是:"+BookDao.getBook(value).getName());
		}
		//设置cookie
		Cookie c=new Cookie("id",book.getId());
		response.addCookie(c);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
5.运行结果:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: