ShowAllBooksServlet.java文件:
package com.itheima.session.app1;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//显示所有可购买的商品,提供购买链接
//提供一个查看购物车的链接
public class ShowAllBooksServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//显示所有可购买的商品,提供购买链接
out.write("本站有以下好书:
");
Map books = BookDB.findAllBooks();
for(Map.Entry me:books.entrySet()){
String url = request.getContextPath()+"/servlet/BuyServlet?id="+me.getKey();
url = response.encodeURL(url);
out.write(me.getValue().getName()+" 购买
");
}
//提供一个查看购物车的链接
String url = request.getContextPath()+"/servlet/ShowCartServlet";
url = response.encodeURL(url);
out.write("查看购物车");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
ShowCartServlet.java文件:
package com.itheima.session.app1;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//显示购物车
public class ShowCartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
List cart = (List)session.getAttribute("cart");
if(cart==null||cart.size()==0){
out.write("您还没有购买任何商品");
return;
}
out.write("你购买的商品如下
");
for(Book b:cart){
out.write(b.getName()+"
");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
BuyServlet.java文件:
package com.itheima.session.app1;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//把书籍放入购物车
public class BuyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
Book b = BookDB.findById(id);
//放到HttpSession中》用List来存
HttpSession session = request.getSession();
List cart = (List) session.getAttribute("cart");
if(cart==null){
cart = new ArrayList();
session.setAttribute("cart", cart);
}
//肯定有购物车
cart.add(b);
String url = request.getContextPath()+"/servlet/ShowAllBooksServlet";
url = response.encodeURL(url);
out.write(b.getName()+"已放入您的购物车继续购物");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Book.java文件:
package com.itheima.session.app1;
import java.io.Serializable;
public class Book implements Serializable{
private String id;
private String name;
private String price;
private String author;
private String description;
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;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price
+ ", author=" + author + ", description=" + description + "]";
}
public Book(String id, String name, String price, String author,
String description) {
super();
this.id = id;
this.name = name;
this.price = price;
this.author = author;
this.description = description;
}
public Book(){}
}
BookDB.java文件:
package com.itheima.session.app1;
import java.util.HashMap;
import java.util.Map;
//模拟数据
public class BookDB {
//key:书的id value:id对应的书
private static Map books = new HashMap();
static{
books.put("1", new Book("1", "葵花宝典", "10", "牛骞", "欲练此功,必须很牛"));
books.put("2", new Book("2", "欲女心经", "2", "薛振宇", "欲练此功,必须清纯"));
books.put("3", new Book("3", "金瓶梅", "20", "刘春龙", "爱情经典"));
books.put("4", new Book("4", "辟邪剑法", "10", "王雷", "欲练此功,必须很牛"));
books.put("5", new Book("5", "JavaWeb开发", "100", "王昭珽", "经典著作"));
}
public static Map findAllBooks(){
return books;
}
public static Book findById(String bookId){
return books.get(bookId);
}
}
运行:http://localhost:8080/Session/servlet/ShowAllBooksServlet
结果:你购买的商品如下
欲女心经
葵花宝典
JavaWeb开发