购物车起始页面 list.jsp
首页很简单,创建了6个物品,并添加图片和超链接,加入购物车后并不转向别的页面,而是重定向到当前页面继续购买。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List</title>
</head>
<%-- <%
System.out.println(request.getRequestURL()); // http://localhost:8080/Web1.1/car/list.jsp
System.out.println(request.getRequestURI()); // /Web1.1/car/list.jsp
System.out.println(request.getContextPath()); // /Web1.1
System.out.println(request.getServletPath()); // /car/list.jsp
%> --%>
<body>
<table border="1" align="center">
<tr>
<td>
<h3>肥皂</h3><img src="/Web1.1/car/img/img%20(1).jpg" width="300" height="200" /><br/>
<a href="/Web1.1/SessionBServlet?name=0">加入购物车</a>
</td>
<td>
<h3>电动车</h3><img src="/Web1.1/car/img/img%20(2).jpg" width="300" height="200" /><br/>
<a href="/Web1.1/SessionBServlet?name=1">加入购物车</a>
</td>
</tr>
<tr>
<td>
<h3>笔记本</h3><img src="/Web1.1/car/img/img%20(3).jpg" width="300" height="200" /><br/>
<a href="/Web1.1/SessionBServlet?name=2">加入购物车</a>
</td>
<td>
<h3>小雨伞</h3><img src="/Web1.1/car/img/img%20(4).jpg" width="300" height="200" /><br/>
<a href="/Web1.1/SessionBServlet?name=3">加入购物车</a>
</td>
</tr>
<tr>
<td>
<h3>八度空间</h3><img src="/Web1.1/car/img/img%20(5).jpg" width="300" height="200" /><br/>
<a href="/Web1.1/SessionBServlet?name=4">加入购物车</a>
</td>
<td>
<h3>床单</h3><img src="/Web1.1/car/img/img%20(6).jpg" width="300" height="200" /><br/>
<a href="/Web1.1/SessionBServlet?name=5">加入购物车</a>
</td>
</tr>
</table>
<a href="/Web1.1/car/car.jsp">查看购物车</a>
</body>
</html>
中间Servlet脚本 SessionBServlet.java
通过上面的首页获取到的name属性,即选择物品的id号,一共六个物品,对应的id从0-5,将获取到的id传给servlet,获取后进行解码,我这边是创建了一个物品数组,对应6个id,得到物品名,加入map集合,如果不存在则创建一个键,如果存在就count加1(count为物品的购买数量),再添加到map中,完成购买过程,跳转到购物车页面。
package Session;
import java.io.IOException;
import java.util.LinkedHashMap;
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;
/**
* Servlet implementation class SessionBServlet
*/
@WebServlet("/SessionBServlet")
public class SessionBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SessionBServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
int intName = Integer.parseInt(name);
String[] products = new String[]{"肥皂","电动车","笔记本","小雨伞","八度空间","床单"};
String productName = products[intName];
HttpSession session = request.getSession();
Map<String, Integer> map = (Map<String, Integer>) session.getAttribute("car");
if (map != null) {
Integer count = map.get(productName);
if (count == null) {
count = 1;
} else {
count++;
}
map.put(productName, count);
} else {
map = new LinkedHashMap<String, Integer>();
map.put(productName, 1);
}
session.setAttribute("car", map);
response.sendRedirect(request.getContextPath() + "/car/list.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
购物车页面 car.jsp
通过servlet传过来的map集合,遍历获取到每一个物品的名称以及需要购买的数量,遍历打印至表格中。
<%@ page language="java" import="java.util.*" import="java.util.Map.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Car</title>
</head>
<%
Map<String, Integer> map = (Map<String, Integer>) session.getAttribute("car");
%>
<body>
<h1 align="center">购物车详情</h1>
<table border="1" align="center">
<tr><th>商品名称</th><th>商品数量</th></tr>
<%
if (map != null && map.size() > 0) {
for (Entry<String, Integer> en : map.entrySet()) {
%>
<tr>
<td><%=en.getKey()%></td>
<td><%=en.getValue()%></td>
</tr>
<%
}
} else {
out.print("您还没添加任何物品");
}
%>
</table>
</body>
</html>
实际效果
首页
购物车