商品页面
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
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 ListBookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调整乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.getSession();
out.print("本网站有如下商品:<br/>");
Map<String,Book> map = Db.getAll();
for(Map.Entry<String, Book> entry : map.entrySet()){
Book book = entry.getValue();
String url = response.encodeURL("/aDay07/servlet/BuyBookServlet?id="+book.getId());
out.print(book.getName() + "<a href='"+url+"' target='_blank'>购买</a><br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
class Db{
private static Map<String,Book> map = new LinkedHashMap<String,Book>();
static{
map.put("1", new Book("1","javaweb开发","老张",39));
map.put("2", new Book("2","jdbc开发","老黎",39));
map.put("3", new Book("3","spring开发","老张",39));
map.put("4", new Book("4","hibernate开发","老方",39));
map.put("5", new Book("5","ajax开发","老毕",39));
}
public static Map getAll(){
return map;
}
}
class Book {
private String id;
private String name;
private String author;
private double price;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String id, String name, String author, double price) {
super();
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
购物车页面
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 ListCartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);//不需要重新建立session了
if(session==null){
out.write("您没有购买任何商品!!");
return;
}
out.write("您购买了如下商品:<br/>");
List<Book> list = (List) session.getAttribute("list");
for(Book book : list){
out.write(book.getName()+"<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
买书页面
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 BuyBookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("id");
Book book=(Book)Db.getAll().get(id);
HttpSession session = request.getSession(false);
//手工以cookie形式发sessionid,以解决关闭浏览器后,上次买的东西还在
String sessionID=session.getId();
Cookie cookie=new Cookie("JSESSIONID",sessionID);
cookie.setMaxAge(3600);
cookie.setPath("/aDay07");
response.addCookie(cookie);
//从session得到用户用于保存所有书的集合(购物车)
List list = (List) session.getAttribute("list");
if(list==null){
list = new ArrayList();
session.setAttribute("list", list);
}
list.add(book);
//request.getRequestDispatcher("/servlet/ListCartServlet").forward(request, response);
//request.getContextPath() /aDay07
String url = response.encodeRedirectURL(request.getContextPath() + "/servlet/ListCartServlet"); //重定向
response.sendRedirect(url);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}