购物车的应用和验证码:

购物车的应用和验证码:

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;

publicclassLogoutServletextends 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);

   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值