案例:購物車應用:
Book:簡單的書籍描述;類html
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);
}
}session
顯示全部的書籍頁面:
ShowAllBooks:
public class ShowAllBooks extends HttpServlet {htm
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>");
}rem
購物車頁面:
ShowCart代碼:
public class ShowCart extends HttpServlet {get
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>");
}
//清空購物車
}servlet
清空購物車頁面:
ClearCart代碼:
public class ClearCart extends HttpServlet {it
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>");
}
io