<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <jsp:forward page="list.do"></jsp:forward>
package web; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.ProductDao; import dao.common.DaoFactory; import entity.Product; @WebServlet(urlPatterns="/list.do") public class ProductServlet extends HttpServlet { /** * */ private static final long serialVersionUID = -7119542366400700217L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { ProductDao pdao = DaoFactory.getInstance("productDao", ProductDao.class); List<Product> list=pdao.findAll(); req.setAttribute("productList", list); req.getRequestDispatcher("list.jsp").forward(req, resp); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<%@ page language="java" import="java.util.*,entity.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'list.jsp' starting page</title> </head> <body> <h3>产品信息列表</h3> <form> <table> <tr> <th>ID</th> <th>Name</th> <th>Price</th> <th>Operation</th> </tr> <% List<Product> list=(List<Product>)request.getAttribute("productList"); int size=list.size()>0?list.size():0; for(int i=0;i<size;i++){ Product p=list.get(i); %> <tr> <td><%=p.getId() %></td> <td><%=p.getName() %></td> <td><%=p.getPrice() %></td> <td><a href="addcar.do?id=<%=p.getId() %>">加入购物车</a></td> </tr> <%} %> </table> </form> </body> </html>
package web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet(urlPatterns="/addcar.do") public class AddCarServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 260407340529396028L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取产品ID String id=req.getParameter("id"); //创建一个HttpSession会话 HttpSession session=req.getSession(); //创建一个用了保存产品信息的map集合,键表示产品id,值表示产品数量 Map<Integer,Integer> map=(Map)session.getAttribute("shopcar"); if(map==null||map.size()==0){ map=new HashMap<Integer,Integer>(); } int productId=Integer.parseInt(id); Integer count=map.get(productId);//根据产品id获取指定产品的数量 if(count!=null){//说明购物车中,已经存在了该款产品 map.put(productId, count+1); }else{ map.put(productId, 1); } session.setAttribute("shopcar", map); req.getRequestDispatcher("/info.jsp").forward(req, resp); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>产品信息提示页面</title> </head> <body> <h3>产品成功添加到购物车</h3> <hr/> <a href="index.jsp">继续购物</a><br/> <a href="viewcar.do">结算</a> </body> </html>
package web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import dao.ProductDao; import dao.common.DaoFactory; import entity.Product; @WebServlet(urlPatterns="/viewcar.do") public class ViewCarServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取当前客户端和服务器端的会话 HttpSession session=req.getSession(); //获取购物车 Map<Integer,Integer> map=(Map<Integer,Integer>)session.getAttribute("shopcar"); //再次创建一个Map集合,键表示产品对象,值表示产品的数量 Map<Product,Integer> map2=new HashMap<>(); // try { ProductDao pDao=DaoFactory.getInstance("productDao", ProductDao.class); //循环获取购物车中,每个产品的信息和数量,并且再次保存到mp2集合中。 for(Entry<Integer, Integer> entry:map.entrySet()){ Product p=pDao.get(entry.getKey()); map2.put(p, entry.getValue()); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } req.setAttribute("map2", map2); req.getRequestDispatcher("/viewcar.jsp").forward(req, resp); } }
<%@ page language="java" import="java.util.Map.*,entity.*,java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'viewcar.jsp' starting page</title> <script type="text/javascript"> function add(i,price){ var count=document.getElementById("count"+i).value; var total=document.getElementById("total").value; document.getElementById("count"+i).value=(parseInt(count)+1); document.getElementById("total").value=(parseInt(total)+price); } function jian(i,price){ var count=document.getElementById("count"+i).value; var total=document.getElementById("total").value; if(count>1){ document.getElementById("count"+i).value=(parseInt(count)-1); document.getElementById("total").value=(parseInt(total)-price); }else{ alert("亲,你真的不要我了吗?"); } } </script> </head> <body> <h3>购物车列表页面</h3> <table> <tr> <th>ID</th> <th>Name</th> <th>Price</th> <th>数量</th> <th>操作</th> </tr> <% Map<Product,Integer> map=(Map<Product,Integer>)request.getAttribute("map2"); int i=0; double total=0; for(Entry<Product, Integer> entry:map.entrySet()){ Product p=entry.getKey(); i++; total+=p.getPrice()*entry.getValue(); %> <tr> <td> <%=i %></td> <td> <%=p.getName() %></td> <td> <%=p.getPrice() %></td> <td> <%=entry.getValue() %></td> <td> <input type="button" value="+" onclick="add(<%=i%>,<%=p.getPrice()%>)"/> <input type="text" name="count" id="count<%=i %>" value="<%=entry.getValue()%>"/> <input type="button" value="-" onclick="jian(<%=i%>,<%=p.getPrice()%>)"/> </td> </tr> <%} %> <tr> <td colspan="4"> 总价:<input type="text" name="total" id="total" value="<%=total %>" disabled="disabled"> <input type="button" value="去结算"/> </td> </tr> </table> </body> </html>