zoukankan      html  css  js  c++  java
  • Session简单实现购物功能

    1,IndexServlet.java 主页Servlet实现主页显示商品

        import java.io.IOException;  
        import java.io.PrintWriter;  
        import java.util.LinkedHashMap;  
        import java.util.Map;  
        import java.util.Set;  
        import javax.servlet.ServletException;  
        import javax.servlet.http.HttpServlet;  
        import javax.servlet.http.HttpServletRequest;  
        import javax.servlet.http.HttpServletResponse;  
          
        //首页:列出所有书  
        public class IndexServlet extends HttpServlet {  
          
            public void doGet(HttpServletRequest request, HttpServletResponse response)  
                    throws ServletException, IOException {  
                response.setContentType("text/html;charset=UTF-8");  
                PrintWriter out = response.getWriter();   
                request.getSession();     
                out.write("本网站有如下书:<br/>");     
                Set<Map.Entry<String,Book>> set = DB.getAll().entrySet();  
                for(Map.Entry<String,Book> me : set){  
                    Book book = me.getValue();  
                    String url = "/day07/servlet/BuyServlet?id=" + book.getId();  
                    url = response.encodeURL(url);  
                    out.println(book.getName()  + "   <a href='"+url+"'>购买</a><br/>");  
                }     
            }  
          
            public void doPost(HttpServletRequest request, HttpServletResponse response)  
                    throws ServletException, IOException {  
                doGet(request, response);  
            }  
          
        }  

    2,BuyServlet.java 该Servlet实现购买页面

        import java.io.IOException;  
        import java.util.ArrayList;  
        import java.util.List;  
        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 BuyServlet extends HttpServlet {  
            public 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();  
                /*Cookie cookie = new Cookie("JSESSIONID",session.getId()); 
                cookie.setMaxAge(30*60); 
                cookie.setPath("/day07"); 
                response.addCookie(cookie);*/     
                List list = (List) session.getAttribute("list");  //得到用户用于保存所有书的容器  
                if(list==null){  
                    list = new ArrayList();  
                    session.setAttribute("list", list);  
                }  
                list.add(book);  
                //request.getRequestDispatcher("/servlet/ListCartServlet").forward(request, response);    
                String url = response.encodeRedirectURL("/day07/servlet/ListCartServlet");//重新URL  
                //System.out.println(url);  
                response.sendRedirect(url);   
            }  
            public void doPost(HttpServletRequest request, HttpServletResponse response)  
                    throws ServletException, IOException {  
                doGet(request, response);  
            }  
          
        }  

    3,ListCartServlet.java 实现将购买的商品通过Session保存发给ListCartServlet,显示你购买的商品

        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 javax.servlet.http.HttpSession;  
        public class ListCartServlet extends HttpServlet {  
          
            public void doGet(HttpServletRequest request, HttpServletResponse response)  
                    throws ServletException, IOException {  
                response.setContentType("text/html;charset=UTF-8");  
                PrintWriter out = response.getWriter();   
                HttpSession session = request.getSession();  
                List<Book> list = (List) session.getAttribute("list");  
                if(list==null || list.size()==0){  
                    out.write("对不起,您还没有购买任何商品!!");  
                    return;  
                }     
                //显示用户买过的商品  
                out.write("您买过如下商品:<br>");  
                for(Book book : list){  
                    out.write(book.getName() + "<br/>");  
                }         
            }  
            public void doPost(HttpServletRequest request, HttpServletResponse response)  
                    throws ServletException, IOException {  
          
                doGet(request, response);  
            }  
          
        }  
  • 相关阅读:
    夺命雷公狗-----React---12--添加类和样式
    夺命雷公狗-----React---11--添加css样式的方法
    夺命雷公狗-----React---10--组建嵌套进行数据遍历
    夺命雷公狗-----React---9--map数据的遍历
    夺命雷公狗-----React---8--react官方提供的组建实现双向绑定
    夺命雷公狗-----React---7--组建的状态props和state
    夺命雷公狗-----React---6--props多属性的传递
    夺命雷公狗-----React---5--props对象的传递
    夺命雷公狗-----React---4--props变量的传递
    夺命雷公狗-----React---3--标签的规则
  • 原文地址:https://www.cnblogs.com/lichone2010/p/3128154.html
Copyright © 2011-2022 走看看