1.建立Book封裝圖書資訊:Book。
2.建立BookDB:模拟資料庫,裡面儲存圖書。
3.提供圖書的購買頁面:ListBookServlet.
4.添加到購物車:purcharseServlet。
5.回顯購物車圖書資訊:CartServlet。
代碼如下:
package 購物車;
public class Book {
private String id;
private String name;
public Book() {
}
public Book(String id, String name) {
this.id = id;
this.name = name;
}
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;
}
}
package 購物車;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class BookDB {
private static Map<String,Book> map = new LinkedHashMap<>();
static {
map.put("1",new Book("1","JAVAWEB"));
map.put("2",new Book("2","JDBC"));
map.put("3",new Book("3","JAVA"));
map.put("4",new Book("4","MATH"));
map.put("5",new Book("5","MVC"));
}
//得到所有的圖書
public static Collection<Book> getAll(){
return map.values();
}
//根據map的key,也就是圖書的id,得到某個圖書。
public static Book getBook(String id){
return map.get(id);
}
}
package 購物車;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
@WebServlet("/list")
public class ListBookServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
//得到所有的圖書
Collection<Book> books = BookDB.getAll();
//回顯到頁面
PrintWriter out = resp.getWriter();
out.write("以下是所售賣的圖書: "+"<br/><br/>");
//周遊集合,把圖書回顯到頁面
for (Book b:books){
String name = b.getName();
String id= b.getId();
//提供一個購買圖書的連結
String url ="<a href='/purcharse?id="+id+"'>點選購買</a>";
out.write("圖書名稱: "+name+" "+url+"<br/><br/>");
}
}
}
package 購物車;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/purcharse")
public class purcharsetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
//擷取圖書資訊
String id =req.getParameter("id");
//判斷id是否存在
if(id==null){
resp.sendRedirect("/list");
return;
}
//圖書已經點選購買了
Book book =BookDB.getBook(id);
//把圖書添加到購物車
HttpSession session = req.getSession();
List<Book> list = (List<Book>) session.getAttribute("cart");
if(list==null){
list=new ArrayList<Book>();
session.setAttribute("cart",list);
}
//把圖書添加到購物車
list.add(book);
//把請求跳轉到cartServlet
//session是基于cookie建立的,為了完成關閉浏覽器,cookie還在 設定時長
Cookie cookie = new Cookie("JESSION",session.getId());
cookie.setMaxAge(60*60*24*7);
cookie.setPath("/");
resp.addCookie(cookie);
//跳轉
resp.sendRedirect("/cart");
}
}
package 購物車;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/cart")
public class cartServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
//得到購物車
List<Book> cart =null;
//得到使用者自己的session
HttpSession session = req.getSession(false);
//定義一個flag 來判斷購物車是否為空
boolean cartflag=true;
if(session==null){
cartflag=false;
}else {
cart= (List<Book>) session.getAttribute("cart");
if(cart==null){
cartflag=false;
}
}
//購物車有商品了
if(!cartflag){
out.write("對不起,你的購物車沒有商品,請去購買點選");
}else {
//購物車有商品
out.write("購買的商品如下:"+"<br><br>");
for (Book b:cart){
out.write("購買的圖書:"+b.getName()+"<br><br>");
}
}
}
}
結果如下:
以下是所售賣的圖書:
圖書名稱: JAVAWEB 點選購買
圖書名稱: JDBC 點選購買
圖書名稱: JAVA 點選購買
圖書名稱: MATH 點選購買
圖書名稱: MVC 點選購買
購買的商品如下:
購買的圖書:JAVAWEB
購買的圖書:JDBC
購買的圖書:JAVA
購買的圖書:JAVAWEB
購買的圖書:MVC
購買的圖書:JAVAWEB