package cn.itcast.estore.web.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import cn.itcast.estore.domain.Product;
import cn.itcast.estore.service.OrderService;
public class FindProductByOrderIdServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取请求参数
String id = request.getParameter("id");
// 2.调用service中的findProductByOrderId()方法
OrderService service = new OrderService();
try {
List<Product> products = service.findProductByOrderId(id);
// 3.将ps转换为json对象
// 不转换imgurl、imgurl_s、num、id
JsonConfig config = new JsonConfig();
config.setExcludes(new String[] { "imgurl", "imgurl_s", "num", "id" });
String json = JSONArray.fromObject(products, config).toString();
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
showOrder.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>订单列表</title>
<script type="text/javascript">
function findProductByOrder_id(order_id) {
var btn = document.getElementById("btn_" + order_id);
var div = document.getElementById("div_" + order_id);
//1.得到xmlHttpRequest对象
var xmlhttp = null;
if (window.XMLHttpRequest) { // code for all new browsers
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {// code for IE5 and IE6
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (btn.value != "关闭") {
div.style.display = "block";
//2.注册回调函数
xmlhttp.onreadystatechange = function() {
//5.在回调函数里处理返回数据
//处理响应数据 当信息全部返回,并且成功
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsonPs = eval("(" + xmlhttp.responseText + ")");
String
table = "<table border='2px' align='center'><tr><td>商品名称</td><td>类别</td><td>价钱</td><td>描述</td></tr>";
for ( var i = 0; i < jsonPs.length; i++) {
table += "<tr><td>" + jsonPs[i].pname + "</td><td>"
+ jsonPs[i].category + "</td><td>"
+ jsonPs[i].price + "</td><td>"
+ jsonPs[i].desc + "</td></tr>";
}
table += "</table>";
div.innerHTML = table;
}
}
//3.设置连接方式,建立服务器连接
xmlhttp.open("get",
"${pageContext.request.contextPath}/findProductByOrderId?id="
+ order_id);
//4.发送请求
xmlhttp.send(null);
btn.value = "关闭";
} else {
btn.value = "查看";
div.style.display = "none";
}
};
</script>
</head>
<body>
<table border="2px" align="center">
<tr><td colspan="8"><a href="${pageContext.request.contextPath}/downLoad">下载销售榜单</a></td></tr>
<tr>
<td>订单编号</td>
<td>订单总价</td>
<td>下单时间</td>
<td>收货地址</td>
<td>收货人</td>
<td>支付状态</td>
<td>订单详情</td>
<td>删除订单</td>
</tr>
<c:forEach items="${orders}" var="order">
<tr>
<td>${order.id}</td>
<td>${order.money}</td>
<td>${order.ordertime}</td>
<td>${order.receiverinfo}</td>
<td>${order.username}【${order.nickname}】</td>
<td><c:if test="${order.paystate==0}">
<a
href="${pageContext.request.contextPath}/pay.jsp?orderid=${order.id}&money=${order.money}">未支付</a>
</c:if> <c:if test="${order.paystate!=0}">
已支付
</c:if></td>
<td><input type="button" value="查看" id="btn_${order.id}"
οnclick="findProductByOrder_id('${order.id}');">
<div id="div_${order.id}"></div></td>
<td><c:if test="${order.paystate==0}">
<a
href="${pageContext.request.contextPath}/cancelOderById?id=${order.id}">删除</a>
</c:if> <c:if test="${order.paystate!=0}">
删除
</c:if></td>
</tr>
</c:forEach>
</table>
</body>
</html>
productInfo.jsp
<%@ 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>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>详细商品介绍</title>
<script type="text/javascript">
function addCartById(id) {
location.href="${pageContext.request.contextPath}/addProductToCartById?id="+id;
};
</script>
</head>
<body>
<table border="1" align="center">
<tr>
<td>商品名称</td>
<td>${p.pname }</td>
</tr>
<tr>
<td>商品类别</td>
<td>${p.category }</td>
</tr>
<tr>
<td>商品数量</td>
<td>${p.num }</td>
</tr>
<tr>
<td>商品价钱</td>
<td>${p.price }</td>
</tr>
<tr>
<td>描述</td>
<td>${p.desc }</td>
</tr>
<tr>
<td >图片</td>
<td ><img src="${pageContext.request.contextPath }${p.imgurl}" height="55" width="55" alt="图片" >
</td>
<td>${p.imgurl }</td>
</tr>
<tr>
<td>图片</td>
<td rowspan="5"><img src="${pageContext.request.contextPath }${p.imgurl_s}" height="55" width="55" >
</td>
<td>${p.imgurl_s }</td>
</tr>
<tr>
<td colspan="2" align="right"><img src="/images/buy.bmp" οnclick="addCartById('${p.id}');"></td>
</tr>
</table>
</body>
</html>
AddCartByIdServlet
package cn.itcast.estore.web.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.itcast.estore.domain.Product;
import cn.itcast.estore.service.ProductService;
public class AddCartByIdServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取购物商品id
String id = request.getParameter("id");
try {
// 2.根据id查询商品
ProductService pservice = new ProductService();
Product p = pservice.findProductById(id);
if (p == null) {
return;
}
// 3.获取购物车,先获取session
HttpSession session = request.getSession();
// 3.1从session获取购物车
Map<Product, Integer> cart = (Map<Product, Integer>) session.getAttribute("cart");
// 3.2 判断购物车是否存在;不存在是第一次访问,创建
if (cart == null) {
// 创建购物车
cart = new HashMap<Product,Integer>();
}
Integer count = cart.get(p);
if (count == null) { // 购物车中没有该商品
count = 1;
} else {
count += 1;
}
cart.put(p, count);
// 将购物车存储到session中.
session.setAttribute("cart", cart);
response.getWriter().write("商品添加成功;<a href='http://www.store.com/showCart.jsp'>查看购物车</a>,继续购物");
return;
} catch (SQLException e) {
e.printStackTrace();
}
}
// 将商品添加到购物车
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}