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;
import cn.csdn.dao.BookDao;
import cn.csdn.domain.Book;
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 bookid= request.getParameter("id");
BookDao bd= new BookDao();
Book book= bd.GetBook(bookid);
// 关闭浏览器后还可进行购买
HttpSession session =request.getSession();
Cookie cookie= new Cookie("JSESSION",session.getId());
cookie.setMaxAge(30*60);
cookie.setPath("/BookHistory");
response.addCookie(cookie);
List list = (List) session.getAttribute("cart");
if(list == null){
list = new ArrayList();
session.setAttribute("cart",list);
}
list.add(book);
// 禁用 cookie 后还可一购买
response.encodeRedirectURL("/BookHistory/servlet/CartDisplayServlet");
response.sendRedirect("url");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package cn.csdn.servlet;
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 cn.csdn.domain.Book;
public class CartDiaplay extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out= response.getWriter();
// 获取 session 的资源
List<Book> list= (List<Book>) request.getSession().getAttribute("cart");
for(Book book:list){
out.print(book.getName()+"<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}package cn.csdn.servlet;
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 cn.csdn.dao.BookDao;
import cn.csdn.domain.Book;
import cn.csdn.domain.User;
public class IndexShoppingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out= response.getWriter();
/*User user = session.getAttribute("user");
if(user!=null){
out.print(" 欢迎您 "+user.getUsername());
}*/
out.print(" 如下商品是: <br/>");
BookDao db= new BookDao();
List <Book> list = db.getAll();
for(Book book:list){
out.print(book.getName()+"<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package cn.csdn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.csdn.dao.UserDao;
import cn.csdn.domain.User;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out= response.getWriter();
String username= request.getParameter("username");
String password= request.getParameter("password");
String checkcode=request.getParameter("checkcode");
System.out.println(checkcode);
String imgcode= (String) request.getSession().getAttribute("imgcode");
if(checkcode!=null && imgcode!=null &&checkcode.equals(imgcode)){
UserDao udao= new UserDao();
User user = udao.find(username, password);
if(user!=null){
request.getSession().setAttribute("user", user);
response.sendRedirect("/BookHistory/servlet/IndexShoppingServlet ");
}
out.print(" 用户名密码错误 !!!!!!");
}else{
out.print(" 验证密码错误!!!! ");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package cn.csdn.servlet;
import java.io.IOException;
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 LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session= request.getSession( false );
if (session == null ){
response.sendRedirect( "/BookHistory/servlet/IndexServletShopping" );
return ;
}
session.removeAttribute( "user" );
response.sendRedirect( "/BookHistory/servlet/IndexServletShopping" );
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}