package com.csdn.cookie;
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 com.csdn.BookDao.BookDao;
import com.csdn.doMain.Book;
public class IndexServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
pw.print("所有的书:<br />");
BookDao bd = new BookDao();
List<Book> list = bd.getAll();
for(Book book:list){
String url=response.encodeURL("/Cookie/BuyServlet?id="+book.getId());
pw.print(book.getName()+"<br/><a href='"+url+"' target='_blank'>购买</a><br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.csdn.cookie;
import java.io.IOException;
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;
import com.csdn.BookDao.BookDao;
import com.csdn.doMain.Book;
public class BuyServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//PrintWriter pw = response.getWriter();
String id=request.getParameter("id");
BookDao bd=new BookDao();
Book book=bd.getBook(id);
HttpSession session=request.getSession();
Cookie cookie=new Cookie("JSESSIONID",session.getId());
cookie.setMaxAge(24*60*60);
cookie.setPath("/Cookie");
response.addCookie(cookie);
List<Book> list=(List<Book>) session.getAttribute("cart");
if(list==null){
list=new ArrayList<Book>();
session.setAttribute("cart", list);
// list.add(0,book);
}
// else{
// for(Book book1:list){
// String id1=book1.getId();
// if(id1==id){
// list.remove(book1);
// //break;
// }
// }
// list.add(0,book);
// }
list.add(0,book);
String url=response.encodeRedirectURL("/Cookie/CartServlet");
response.sendRedirect(url);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.csdn.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
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;
import com.csdn.doMain.Book;
public class CartServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
HttpSession session=request.getSession();
List<Book> list=(List) session.getAttribute("cart");
Iterator<Book> it=list.iterator();
while(it.hasNext()){
Book book=(Book) it.next();
pw.print(book.getName()+"<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}