购物车的应用和验证码:
package cn.csdn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
importjavax.servlet.ServletException;
import javax.servlet.http.Cookie;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
import cn.csdn.dao.BookDao;
import cn.csdn.domain.Book;
public class BuyServlet extendsHttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, 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= newCookie("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");
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doGet(request,response);
}
}
packagecn.csdn.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.List;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcn.csdn.domain.Book;
publicclass CartDiaplay extends HttpServlet {
public void doGet(HttpServletRequestrequest, 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(HttpServletRequestrequest, HttpServletResponse response)
throws ServletException, IOException{
doGet(request, response);
}
}packagecn.csdn.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.List;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcn.csdn.dao.BookDao;
importcn.csdn.domain.Book;
importcn.csdn.domain.User;
publicclass IndexShoppingServlet extends HttpServlet {
public void doGet(HttpServletRequestrequest, 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(HttpServletRequestrequest, HttpServletResponse response)
throws ServletException, IOException{
doGet(request, response);
}
}
packagecn.csdn.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcn.csdn.dao.UserDao;
importcn.csdn.domain.User;
publicclass LoginServlet extends HttpServlet {
public void doGet(HttpServletRequestrequest, 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");
Stringcheckcode=request.getParameter("checkcode");
System.out.println(checkcode);
String imgcode= (String)request.getSession().getAttribute("imgcode");
if(checkcode!=null &&imgcode!=null &&checkcode.equals(imgcode)){
UserDaoudao= 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(HttpServletRequestrequest, HttpServletResponse response)
throws ServletException, IOException{
doGet(request, response);
}
}
package cn.csdn.servlet;
import java.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
publicclass LogoutServletextends HttpServlet {
publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
HttpSessionsession= request.getSession(false);
if(session == null){
response.sendRedirect("/BookHistory/servlet/IndexServletShopping");
return;
}
session.removeAttribute("user");
response.sendRedirect("/BookHistory/servlet/IndexServletShopping");
}
publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
doGet(request,response);
}
}