package com.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.GoodsDao;
import com.dao.OrderformDao;
import com.dao.ShopcarDao;
import com.dao.TempDao;
import com.toolsbean.StringHandler;
import com.valuebean.OrderformBean;
import com.valuebean.ShopcarBean;
import com.valuebean.UserBean;
public class ShopcarServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String servletpath=request.getServletPath();
if("/buy".equals(servletpath))
buy(request,response);
else if("/showshopcar".equals(servletpath))
showshopcar(request,response);
else if("/remove".equals(servletpath))
remove(request,response);
else if("/clearshopcar".equals(servletpath))
clear(request,response);
else if("/submitshopcar".equals(servletpath))
submitDispatcher(request,response);
else if("/createorderform".equals(servletpath))
createorderform(request,response);
}
/** @功能:购买商品 */
protected void buy(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String message="";
Integer buygoodsid=StringHandler.strToint(request.getParameter("buygoodsId"));
if(buygoodsid==null){
message="<li>商品ID值错误!</li>";
message+="<a href='window.history.go(-1)'>返回</a>";
}
else{
try {
Date now=new Date(); //获取当前时间
TempDao tempDao=new TempDao();
String shopcarid=seeshopcarcookie(request,response); //查询客户端Cookie中是否保存了一个购物车ID值
if(shopcarid==null||shopcarid.equals("")||!tempDao.isexist(shopcarid)){ //没有保存
shopcarid=addshopcarcookie(request,response,now); //生成一个购物车ID保存到客户端Cookie中,并返回该ID值
tempDao.saveShopcarCreateTime(shopcarid, StringHandler.timeTostr(now)); //记录该购物车ID和创建时间到数据表中
}
int i=-1;
ShopcarDao shopcarDao=new ShopcarDao();
Object[] params={shopcarid,buygoodsid};
if(shopcarDao.isBuy(params)) //如果已经购买了该商品
i=shopcarDao.addBuyNum(params); //更新商品购买数量
else{ //没有购买该商品
params=new Object[]{shopcarid,buygoodsid,1};
i=shopcarDao.addBuyGoods(params); //添加该商品到购物车(tb_shopcar数据表)中
}
shopcarDao.closed();
if(i<=0)
message="<li>添加商品到购物车失败!</li><br>";
else
message="<li>添加商品到购物车成功!</li><br>";
message+="<a href='javascript:window.history.go(-1)'>返回</a>";
message+=" ";
message+="<a href='showshopcar'>查看购物车<a/>";
} catch (SQLException e) {
e.printStackTrace();
}
}
request.setAttribute("message",message);
RequestDispatcher rd=request.getRequestDispatcher("/message.jsp");
rd.forward(request,response);
}
private String seeshopcarcookie(HttpServletRequest request,HttpServletResponse response){
String webname=request.getContextPath();
webname=webname.substring(1);
Cookie[] coks=request.getCookies();
String shopcarid="";
int i=0;
for(;i<coks.length;i++){
Cookie icok=coks[i];
if(icok.getName().equals(webname+".usershopcar")){
shopcarid=icok.getValue();
break;
}
}
return shopcarid;
}
private String addshopcarcookie(HttpServletRequest request,HttpServletResponse response,Date date){
String webname=request.getContextPath();
webname=webname.substring(1);
String shopcarid="car"+StringHandler.getSerial(date);
Cookie shopcar=new Cookie(webname+".usershopcar",shopcarid);
shopcar.setPath("/");
int maxage=60*60*24*3; //设置cookie的有效期为3天
shopcar.setMaxAge(maxage);
response.addCookie(shopcar);
return shopcarid;
}
protected void showshopcar(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String shopcarid=seeshopcarcookie(request,response);
if(shopcarid!=null&&!shopcarid.equals("")){
ShopcarBean shopcar=new ShopcarDao().getShopcar(shopcarid);
request.setAttribute("shopcar",shopcar);
}
RequestDispatcher rd=request.getRequestDispatcher("/showShopcar.jsp");
rd.forward(request,response);
}
protected void remove(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
Integer goodsId=StringHandler.strToint(request.getParameter("goodsId"));
String shopcarid=seeshopcarcookie(request,response);
if(shopcarid!=null&&!shopcarid.equals("")&&goodsId!=null){
ShopcarDao shopcarDao=new ShopcarDao();
int i=shopcarDao.deleteGoods(shopcarid,goodsId);
if(i<=0)
request.setAttribute("message","●删除商品失败!");
else
request.setAttribute("message","●删除商品成功!");
shopcarDao.closed();
}
showshopcar(request,response);
}
protected void clear(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String shopcarid=seeshopcarcookie(request,response);
if(shopcarid!=null&&!shopcarid.equals("")){
ShopcarDao shopcarDao=new ShopcarDao();
int i=shopcarDao.clearShopcar(shopcarid);
if(i<=0)
request.setAttribute("message","●清空购物车失败!");
else
request.setAttribute("message","●清空购物车成功!");
shopcarDao.closed();
}
RequestDispatcher rd=request.getRequestDispatcher("/showShopcar.jsp");
rd.forward(request,response);
}
protected void submitDispatcher(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
String whichsubmit=request.getParameter("whichsubmit");
if(whichsubmit==null)
payforMoney(request,response);
else
updatebuyNum(request,response);
}
private void payforMoney(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
if(shopcar_validateBuyNum(request, response)){
String buygoodsids=StringHandler.ArrayToString(request.getParameterValues("buygoodsids"));
String buygoodsnum=StringHandler.ArrayToString(request.getParameterValues("buygoodsnum"));
request.setAttribute("buygoodsids", buygoodsids);
request.setAttribute("buygoodsnum", buygoodsnum);
RequestDispatcher rd=request.getRequestDispatcher("/fillOrderform.jsp");
rd.forward(request,response);
}
else
showshopcar(request,response);
}
protected void updatebuyNum(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
shopcar_validateBuyNum(request, response);
showshopcar(request,response);
}
private boolean shopcar_validateBuyNum(HttpServletRequest request,HttpServletResponse response){
boolean mark=true;
String[] goodsStoreNums=request.getParameterValues("buygoodsstorenum");
String[] buyNums=request.getParameterValues("buygoodsnum");
String[] goodsIds=request.getParameterValues("buygoodsids");
String shopcarid=seeshopcarcookie(request,response);
if(goodsIds!=null&&goodsIds.length!=0&&shopcarid!=null){
Map messages=new HashMap();
Object[] params=new Object[3];
ShopcarDao shopcarDao=new ShopcarDao();
for(int i=0;i<goodsIds.length;i++){
int int_buyNum=Integer.parseInt(buyNums[i]);
int int_goodsStoreNums=Integer.parseInt(goodsStoreNums[i]);
if(int_buyNum>int_goodsStoreNums){
mark=false;
messages.
没有合适的资源?快使用搜索试试~ 我知道了~
JSP在线支付模块-源代码
共197个文件
jpg:52个
java:46个
class:46个
4星 · 超过85%的资源 需积分: 19 104 下载量 39 浏览量
2011-06-11
17:45:33
上传
评论 2
收藏 1.88MB RAR 举报
温馨提示
本实现提供了两种支付方式,第一种是使用支付宝平台提供的接口实现在线支付,第二种是使用工商银行提供的接口实现在线支付。这里我们只介绍第一种的操作方法。
资源推荐
资源详情
资源评论
收起资源包目录
JSP在线支付模块-源代码 (197个子文件)
ShopcarServlet.class 13KB
ShopcarServlet.class 13KB
GoodsServlet.class 8KB
GoodsServlet.class 8KB
MyPayment.class 5KB
PageBar.class 5KB
PageBar.class 5KB
GoodsDao.class 5KB
GoodsDao.class 5KB
OrderformDao.class 5KB
OrderformDao.class 5KB
ShopcarDao.class 4KB
ShopcarDao.class 4KB
PaymentOrdersServlet.class 4KB
Payment.class 4KB
OrderformBean.class 3KB
OrderformBean.class 3KB
StringHandler.class 3KB
StringHandler.class 3KB
DB.class 3KB
DB.class 3KB
SignatureHelper_return.class 3KB
SignatureHelper.class 3KB
GoodsBean.class 2KB
GoodsBean.class 2KB
LogXServlet.class 2KB
LogXServlet.class 2KB
SubmitFilter.class 2KB
SubmitFilter.class 2KB
SuperDao.class 2KB
SuperDao.class 2KB
LoginFilter.class 2KB
LoginFilter.class 2KB
Md5Encrypt.class 2KB
TempDao.class 2KB
TempDao.class 2KB
UserDao.class 1KB
UserDao.class 1KB
CharacterEncodingFilter.class 1KB
CharacterEncodingFilter.class 1KB
CheckURL.class 1KB
ShopcarBean.class 1KB
ShopcarBean.class 1KB
UserBean.class 926B
UserBean.class 926B
MyConversion.class 862B
style.css 1016B
style.css 1016B
程序使用说明.doc 946KB
alipay_bwrx.gif 5KB
ShopcarServlet.java 13KB
ShopcarServlet.java 13KB
GoodsServlet.java 8KB
GoodsServlet.java 8KB
MyPayment.java 5KB
PaymentOrdersServlet.java 5KB
PageBar.java 4KB
PageBar.java 4KB
GoodsDao.java 4KB
GoodsDao.java 4KB
OrderformBean.java 3KB
OrderformBean.java 3KB
OrderformDao.java 3KB
OrderformDao.java 3KB
ShopcarDao.java 3KB
ShopcarDao.java 3KB
Payment.java 3KB
Md5Encrypt.java 3KB
DB.java 2KB
DB.java 2KB
StringHandler.java 2KB
StringHandler.java 2KB
GoodsBean.java 2KB
GoodsBean.java 2KB
SignatureHelper.java 2KB
SignatureHelper_return.java 2KB
SuperDao.java 1KB
LoginFilter.java 1KB
LoginFilter.java 1KB
SubmitFilter.java 1KB
SubmitFilter.java 1KB
LogXServlet.java 1KB
LogXServlet.java 1KB
TempDao.java 1KB
SuperDao.java 1KB
TempDao.java 973B
UserDao.java 970B
CharacterEncodingFilter.java 819B
CharacterEncodingFilter.java 819B
CheckURL.java 795B
UserDao.java 741B
ShopcarBean.java 728B
ShopcarBean.java 728B
UserBean.java 489B
UserBean.java 489B
MyConversion.java 396B
viewpic9.jpg 40KB
messbk.jpg 36KB
messbk.jpg 36KB
login_bk.jpg 34KB
共 197 条
- 1
- 2
月饼哥哥
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Java毕设项目:基于spring+mybatis+maven+mysql实现的企业财务管理系统【含源码+数据库+开题报告+毕业论文+答辩PPT】
- Sigrity Power SI 仿真分析教程与实例分析.rar
- Java毕设项目:基于spring+mybatis+maven+mysql实现的海鲜自助餐厅管理系统【含源码+数据库+毕业论文】
- Java毕设项目:基于spring+mybatis+maven+mysql实现的端游账号销售管理系统分前后台【含源码+数据库+毕业论文】
- Emby Docker镜像-4.8.8版本
- Java毕设项目:基于spring+mybatis+maven+mysql实现的校园代购服务订单管理系统分前后台【含源码+数据库+毕业论文】
- MySQL Docker镜像-v9.0.0
- 金铲铲S13双城之战自动拿牌助手2.0
- postgres Docker镜像-v16.4
- zookeeper Docker镜像-v3.9.2
- nvdiffrast frpc-linux-amd64
- sonarqube Docker镜像-v9.9.5-developer
- 华为AP无线接入控制器学习资料
- 毕设238912734017234
- adminer Docker镜像-v4.8.1
- 恒温加热台外壳3d打印件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页