package com.itheima.cookie.app3;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//显示所有的商品,并且提供查看明细的链接
//显示是最近的浏览记录:3
public class ShowAllBookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//显示所有的商品,并且提供查看明细的链接
out.write("<h1>本站有以下好书<h1/>");
Map<String, Book> books=BookDB.findAllBooks();
for(Map.Entry<String, Book> me:books.entrySet()){
out.write(me.getValue().getName()+" <a href='"+request.getContextPath()
+"/servlet/ShowDetailServlet?id="+me.getKey()+"' target='_blank'>查看明细<a/><br/>");
}
//显示是最近的浏览记录:3 bookHistory:2-1-3
out.write("<hr/>");
out.write("最近的浏览历史记录:<br>");
Cookie [] cs=request.getCookies();
for(int i=0;cs!=null& i<cs.length;i++){
if("bookHistory".equals(cs[i].getName())){
String value=cs[i].getValue();
String ids[]=value.split("\\-");
for(String id:ids){
Book book=BookDB.findBookById(id);
out.write(book.getName()+"<br/>");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
|
|