欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsp实现简单购物车过程

程序员文章站 2022-03-15 09:45:51
...

map介绍

//定义
Map<String, Double> map1 = new HashMap<String, Double>();
//存储
map1.put("苹果手机9", 7000.0);
//获值
map1.get("苹果手机9");

 

页面介绍

buy.jsp

复选框写出可选商品。

processbuy.jsp

1、传入map,如果为空,则初始化为0

2、获取选择的物品

3、循环,置物品的购买数量

4、把数量保存到session

5、转发到show.jsp

show.jsp

显示购买的物品数量,以及价格。

 

源代码

buy.jsp

<h2 align="center">商品列表</h2>
    <div align="center">
      <form action="processbuy.jsp" method="post">
        <table border="0" cellspacing="30">
          <tr>
            <th>商品名称</th>
            <th>购买</th>
            <th>价格</th>
          </tr>
          <tr>
          	<td>笔记本</td>
            <td><input type="checkbox" name="item" value="computer"></td>
            <td>¥5000</td>
          </tr>
          <tr>
          	<td>汽车</td>
            <td><input type="checkbox" name="item" value="car"></td>
            <td>¥200000</td>
          </tr>
          <tr>
          	<td>香水</td>
            <td><input type="checkbox" name="item" value="flower"></td>
            <td>¥500</td>
          </tr>
          <tr>
          	<td>书籍</td>
            <td><input type="checkbox" name="item" value="book"></td>
            <td>¥20</td>
          </tr>
		</table>
		
		<input type="submit" value="购买">      
      </form>
    </div>

processbuy.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

	request.setCharacterEncoding("utf-8");
	
	Map<String, Integer> map1 = (Map<String, Integer>)session.getAttribute("map1");
	if(map1 == null){
		map1 = new HashMap<String, Integer>();
		map1.put("笔记本", 0);
		map1.put("汽车", 0);
		map1.put("香水", 0);
		map1.put("书籍", 0);
	}
	
	String[] buys = request.getParameterValues("item");
	
	for(String item:buys){
		if(item.equals("computer")){
			int num1 = map1.get("笔记本").intValue();
			map1.put("笔记本", num1+1);
		}
		else if(item.equals("car")){
			int num2 = map1.get("汽车").intValue();
			map1.put("汽车", num2+1);
		}
		else if(item.equals("flower")){
			int num3 = map1.get("香水").intValue();
			map1.put("香水", num3+1);
		}
		else{
			int num4 = map1.get("书籍").intValue();
			map1.put("书籍", num4+1);
		}
	}

	session.setAttribute("map1", map1);
	request.getRequestDispatcher("show.jsp").forward(request, response);
%>

show.jsp

    <div align="center">
      <h2>结算</h2>
      <%
    	Map<String, Integer> map1 = (Map<String, Integer>)session.getAttribute("map1");
      %>
      	
      	<table border="0" cellspacing="30">
          <tr>
            <th>商品名称</th>
            <th>数量</th>
            <th>总价</th>
          </tr>
          <tr>
          	<td>笔记本</td>
            <td><%=map1.get("笔记本") %></td>
            <td><%=map1.get("笔记本") %>*5000</td>
          </tr>
          <tr>
          	<td>汽车</td>
            <td><%=map1.get("汽车")%></td>
            <td><%=map1.get("汽车")%>*200000</td>
          </tr>
          <tr>
          	<td>香水</td>
            <td><%=map1.get("香水")%></td>
            <td><%=map1.get("香水")%>*500</td>
          </tr>
          <tr>
          	<td>书籍</td>
            <td><%=map1.get("书籍") %></td>
            <td><%=map1.get("书籍") %>*20</td>
          </tr>
		</table>
		<p>总金额:<%=map1.get("笔记本")*5000+map1.get("汽车")*200000+map1.get("香水")*500+map1.get("书籍")*20 %></p>
		<p><a href="buy.jsp">继续购买</a></p>
    </div>

 

相关标签: jsp