java session 保存买过的商品信息

上代码
商品的信息

/**
 *所有商品的信息
 */
public class ShappingLists extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
                request.getSession();//创建session
        out.write("本网站有如下书:
"); //展示数据 Set> set = DB.getAll().entrySet(); for(Map.Entry me : set){ Book book = me.getValue(); String url = "/Demo2/BuyProductsServlet?id=" + book.getId(); //给url带上sessionId防止浏览器禁用cookies url = response.encodeURL(url); out.println(book.getName() + " 购买
"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

点击了后进入的存session的界面(过度的界面)

/**
 * 购买商品的具体界面
 */
public class BuyProductsServlet extends HttpServlet {
     
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String id = request.getParameter("id");
            Book book = (Book) DB.getAll().get(id);  //得到用户想买的书
            
            HttpSession session = request.getSession();
            
            
            List list = (List) session.getAttribute(ShappingListServlet.session_key);  //得到用户用于保存所有书的容器
            if(list==null){
                list = new ArrayList();
                session.setAttribute(ShappingListServlet.session_key, list);
            }
            list.add(book);
            
            //服务器跳转的方法
            //request.getRequestDispatcher("/servlet/ListCartServlet").forward(request, response);
            
            //浏览器的跳转
            String url = response.encodeRedirectURL("/Demo2/ShappingListServlet");
            System.out.println(url);
            response.sendRedirect(url);
    }   
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

展示够买过的商品信息

/**
 *  利用 session 显示购买过的商品
 */
public class ShappingListServlet extends HttpServlet {

     /**
      * session 是服务端的技术   当用户点击了购买商品后 在本地记录下该商品的信息。挑战到购买界面后进行 展示商品的信息
      * 
      * 注意 一个会话只会保存一份serssion  但是 ie8除外 ,ie8不管是打开几个浏览器都是保存的一个serssion
      */
    
    public static final String session_key="list";
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //显示用户购买过的商品
        out.write("你购买过的商品有:
"); HttpSession session=request.getSession(); List list=(List) session.getAttribute(session_key); if(list==null||list.size()==0) { out.write("您还没有买过什么商品呢"); return; } for (Book book : list) { out.write("商品有"+book.getName()+"
"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //模拟数据库 public static class DB { //有序的 map public static LinkedHashMap map=new LinkedHashMap(); static { //静态代码块 用于初始化 map的信息 map.put("1", new Book("1","javaweb开发","老张","一本好书")); map.put("2", new Book("2","spring开发","老黎","一本好书")); map.put("3", new Book("3","hibernate开发","老佟","一本好书")); map.put("4", new Book("4","struts开发","老毕","一本好书")); map.put("5", new Book("5","ajax开发","老张","一本好书")); } public static Map getAll(){ return map; } } //商品的信息 public static class Book { private String id; private String name; private String author;//作者 private String description; //描述 public Book() { super(); // TODO Auto-generated constructor stub } public Book(String id, String name, String author, String description) { super(); this.id = id; this.name = name; this.author = author; this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } }

星期六 过去了 晚安了

你可能感兴趣的:(java session 保存买过的商品信息)