ActiveUserServlet.java
package cn.itcast.itcaststore.web.servlet.client;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.itcaststore.exception.ActiveUserException;
import cn.itcast.itcaststore.service.UserService;
@SuppressWarnings("serial")
public class ActiveUserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取激活码
String activeCode = request.getParameter("activeCode");
// 2.调用service中激活用户操作
UserService service = new UserService();
try {
service.activeUser(activeCode);
response.sendRedirect(request.getContextPath() + "/client/activesuccess.jsp");
return;
} catch (ActiveUserException e) {
e.printStackTrace();
response.getWriter().write(e.getMessage());
return;
}
}
}
AddCartServlet.java
package cn.itcast.itcaststore.web.servlet.client;
import java.io.IOException;
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.itcaststore.domain.Product;
import cn.itcast.itcaststore.exception.FindProductByIdException;
import cn.itcast.itcaststore.service.ProductService;
public class AddCartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.得到商品id
String id = request.getParameter("id");
// 2.调用service层方法,根据id查找商品
ProductService service = new ProductService();
try {
Product p = service.findProductById(id);
//3.将商品添加到购物车
//3.1获得session对象
HttpSession session = request.getSession();
//3.2从session中获取购物车对象
Map<Product, Integer> cart = (Map<Product, Integer>)session.getAttribute("cart");
//3.3如果购物车为null,说明没有商品存储在购物车中,创建出购物车
if (cart == null) {
cart = new HashMap<Product, Integer>();
}
//3.4向购物车中添加商品
Integer count = cart.put(p, 1);
//3.5如果商品数量不为空,则商品数量+1,否则添加新的商品信息
if (count != null) {
cart.put(p, count + 1);
}
session.setAttribute("cart", cart);
response.sendRedirect(request.getContextPath() + "/client/cart.jsp");
return;
} catch (FindProductByIdException e) {
e.printStackTrace();
}
}
}
CallBackServlet.java
package cn.itcast.itcaststore.web.servlet.client;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.itcaststore.service.OrderService;
import cn.itcast.itcaststore.utils.PaymentUtil;
public class CallBackServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得回调所有数据
String p1_MerId = request.getParameter("p1_MerId");
String r0_Cmd = request.getParameter("r0_Cmd");
String r1_Code = request.getParameter("r1_Code");
String r2_TrxId = request.getParameter("r2_TrxId");
String r3_Amt = request.getParameter("r3_Amt");
String r4_Cur = request.getParameter("r4_Cur");
String r5_Pid = request.getParameter("r5_Pid");
String r6_Order = request.getParameter("r6_Order");
String r7_Uid = request.getParameter("r7_Uid");
String r8_MP = request.getParameter("r8_MP");
String r9_BType = request.getParameter("r9_BType");
String rb_BankId = request.getParameter("rb_BankId");
String ro_BankOrderId = request.getParameter("ro_BankOrderId");
String rp_PayDate = request.getParameter("rp_PayDate");
String rq_CardNo = request.getParameter("rq_CardNo");
String ru_Trxtime = request.getParameter("ru_Trxtime");
// 身份校验 --- 判断是不是支付公司通知你
String hmac = request.getParameter("hmac");
String keyValue = ResourceBundle.getBundle("merchantInfo").getString(
"keyValue");
// 自己对上面数据进行加密 --- 比较支付公司发过来hamc
boolean isValid = PaymentUtil.verifyCallback(hmac, p1_MerId, r0_Cmd,
r1_Code, r2_TrxId, r3_Amt, r4_Cur, r5_Pid, r6_Order, r7_Uid,
r8_MP, r9_BType, keyValue);
if (isValid) {
if ("1".equals(r9_BType) || "2".equals(r9_BType)) { // 判断正确支付.
// 响应数据有效--完成修改订单状态操作
response.getWriter().print("success");
OrderService service = new OrderService();
// 根据订单号修改订单状态
try {
service.updateState(r6_Order);
} catch (Exception e) {
e.printStackTrace();
response.getWriter().write("修改订单状态失败");
}
}
} else {
// 数据无效
response.getWriter().write("数据被篡改!");
}
}
}
ChangeCartServlet.java
package cn.itcast.itcaststore.web.servlet.client;
import java.io.IOException;
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.itcaststor