码迷,mamicode.com
首页 > 其他好文 > 详细

ShowAllBooksServlet

时间:2016-10-21 00:50:40      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:


public class ShowAllBooksServlet 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("本网站有以下好书:<br/>");
Map<String, Book> books = DBUtil.findAllBooks();
for (Map.Entry<String, Book> book : books.entrySet()) {
out.write("<a href=‘"+request.getContextPath()+"/servlet/showBookDetail?id="+book.getKey()+"‘ target=‘_blank‘>"+book.getValue().getName()+"</a><br/>");
}


out.write("<hr/>您最近浏览过的书有:<br/>");
Cookie[] cookies = request.getCookies();
for (int i = 0;cookies!=null && i < cookies.length; i++) {
if("historyBookId".equals(cookies[i].getName())){//判断是否有historyBookId的cookie
String value = cookies[i].getValue(); // 1-2-3
String[] ids = value.split("-");
for (int j = 0; j < ids.length; j++) {
Book book = DBUtil.findBookById(ids[j]);//根据ID得到指定的书
out.print(book.getName()+"<br/>");
}
}
}

}

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

}

ShowAllBooksServlet

标签:

原文地址:http://www.cnblogs.com/wbh-hello/p/5982838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!