案例:购物车应用:
Book:简单的书籍描述;类
Bookdb代码:
public class Bookdb {
private static Map<String,Book> books=new HashMap<String, Book>();
static{
books.put("1", new Book("1", "玉女心经", "hehe"));
books.put("2", new Book("2", "辟邪剑谱", "haha"));
books.put("3", new Book("3", "葵花宝典", "xixi"));
}
//获取所有书籍的方法
public static Map<String, Book> findAllBooks(){
return books;
}
public static Book findBookbyid(String bookId){
return books.get(bookId);
}
}
显示所有的书籍页面:
ShowAllBooks:
public class ShowAllBooks 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/>");
//获取books集合,遍历
Map<String, Book> me=Bookdb.findAllBooks();
//获取集合中的value值,即book对象
Set<Map.Entry<String, Book>> se=me.entrySet();
//获取book对象的name属性
//打印name属性,并创建购买连接,连接中需要将书籍的id属性返回给购买成功界面
for(Map.Entry<String, Book> books:se){
out.write(books.getValue().getName()+"<a href='"+request.getContextPath()+"/servlet/BuyBook?bookId="+books.getKey()+"'>购买</a>"+"<br/>");
}
//查看购物车
out.write("<a href='"+request.getContextPath()+"/servlet/ShowCart'>查看购物车</a>");
}
存入页面代码:
BuyBook:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//将商品放到购物车,购物车应当是一个集合
String bookId=request.getParameter("bookId");
//获取到需要购买的书籍对象
Book book=Bookdb.findBookbyid(bookId);
//必须要现有一个购物车
HttpSession session=request.getSession();
List<Book> cart=(List<Book>) session.getAttribute("cart");
if(cart==null){
cart=new ArrayList<Book>();
session.setAttribute("cart", cart);
}
//到这里,必定有购物车
cart.add(book);
out.write("已经放入购物车"+"<a href='"+request.getContextPath()+"/servlet/ShowAllBooks'>继续购物</a>");
}
购物车页面:
ShowCart代码:
public class ShowCart extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//购物车
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//获取到购物车内的所有book对象,遍历
HttpSession session=request.getSession();
List<Book> list=(List<Book>) session.getAttribute("cart");
if(list==null){
out.write("请先挑选商品"+"<br/>");
out.write("<a href='"+request.getContextPath()+"/servlet/ShowAllBooks'>返回购物界面</a>"+"<br/>");
}else{
out.write("您放入购物车的商品如下:"+"<br/>");
for(Book books:list){
out.write(books.getName()+"<br/>");
}
out.write("<a href='"+request.getContextPath()+"/servlet/ShowAllBooks'>返回购物界面</a>"+"<br/>");
out.write("<a href='"+request.getContextPath()+"/servlet/ClearCart'>清空购物车</a>");
}
//清空购物车
}
清空购物车页面:
ClearCart代码:
public class ClearCart extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//清空购物车
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//获取session对象,将session删除
HttpSession session=request.getSession();
//清空session
session.removeAttribute("cart");
out.write("已经清空您的购物车"+"<br/>");
out.write("<a href='"+request.getContextPath()+"/servlet/ShowAllBooks'>返回购物界面</a>");
}