ch06 会话及会话技术(杜老师)


1. 用户上次访问时间(LastAccessServlet01_du)

     Cookie[] cookies = request.getCookies(); 
     if ("lastAccess".equals(cookies[i].getName()));cookies[i].getValue(); 
     Cookie cookie = new Cookie("lastAccess", new Date().toLocaleString()); 
     response.addCookie(cookie);     

2. 实现图书购物车(ListBookServlet02_du)

 PurchaseServlet02_du.java 所选图书加入购物车
        List cart = (List) session.getAttribute("cart");
        if (cart == null) {
            // 首次购买,为用户创建一个购物车(List集合模拟购物车)
            cart = new ArrayList();
            // 将购物城存入Session对象
            session.setAttribute("cart", cart);
        }
        // 将商品放入购物车
        cart.add(book);
        // 创建Cookie存放Session的标识号
        Cookie cookie = new Cookie("JSESSIONID", session.getId());
        cookie.setMaxAge(60 * 30);
        cookie.setPath("/ch06_du");
        resp.addCookie(cookie);

3. 用户登录(Login03_du.html )

 
     1. <form action="/ch06_du/LoginServlet03_du" method="post">
     2. String username = request.getParameter("username");
        String password = request.getParameter("password");
        PrintWriter pw = response.getWriter();
          //假设正确的用户名 是dzjiang 密码是123456
        if (("dzjiang").equals(username) && ("123456").equals(password)) {
            User03_du user = new User03_du();
            user.setUsername(username);
            user.setPassword(password);
            request.getSession().setAttribute("user", user);
            response.sendRedirect("/ch06_du/IndexServlet03_du");
        } else {
            pw.write("用户名或密码错误,登录失败");
            response.setHeader("Refresh","3;URL=/ch06_du/Login03_du.html"); 
            //response.sendRedirect("/ch06_du/Login03_du.html");
        }
     3. HttpSession session = request.getSession();
        User03_du user = (User03_du) session.getAttribute("user");
        if (user == null) {
            response.getWriter().print(
                    "您还没有登录,请登录");
        } else {
            response.getWriter().print("您已登录,欢迎你," + user.getUsername() + "!");
            response.getWriter().print(
                    "退出");
            // 创建Cookie存放Session的标识号
            Cookie cookie = new Cookie("JSESSIONID", session.getId());
            cookie.setMaxAge(60 * 30);
            cookie.setPath("/ch06_du/03");
            response.addCookie(cookie);
        }
    4.  // 将Session对象中的User对象移除
        request.getSession().removeAttribute("user");
        response.sendRedirect("/ch06_du/IndexServlet03_du");
    

4. 带验证码的用户登录(Login04_du.html

        //创建内存图象并获得其图形上下文
        BufferedImage image = 
            new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 
        Graphics g = image.getGraphics();
        char [] rands = generateCheckCode(); //产生随机的认证码
        drawBackground(g);       //产生背景图像
        drawRands(g,rands);      
        g.dispose();             //结束图像的绘制过程,完成图像
        //将图像输出到客户端
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", bos);
        byte [] buf = bos.toByteArray();
        response.setContentLength(buf.length);
        //下面的语句也可写成:bos.writeTo(sos);
        sos.write(buf);
        bos.close();
        sos.close();
        //将当前验证码存入到Session中
        session.setAttribute("check_code",new String(rands)); 

返回 制作