package com.xmut.estore.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
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 com.xmut.estore.domain.ShoppingCart;
import com.xmut.estore.web.EStoreWebUtils;
import com.xmut.estore.service.UserService;
import com.xmut.estore.domain.Account;
import com.xmut.estore.domain.ShoppingCartItem;
import com.xmut.estore.domain.User;
import com.xmut.estore.service.AccountService;
import com.xmut.estore.web.EStoreWebUtils;
import com.google.gson.Gson;
import com.xmut.estore.domain.Computer;
import com.xmut.estore.service.ComputerService;
import com.xmut.estore.web.CriteriaComputer;
import com.xmut.estore.web.Page;
public class ComputerServlet extends HttpServlet {
private ComputerService computerService = new ComputerService();
private UserService userService = new UserService();
private AccountService accountService = new AccountService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
//method反射机制调用方法
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodName = request.getParameter("method");
try {
// 利用反射获取方法
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
// 执行相应的方法
method.setAccessible(true);
method.invoke(this, request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void cash(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 简单验证: 验证表单域的值是否符合基本的规范: 是否为空, 是否可以转为 int 类型, 是否是一个 email. 不需要进行查询
//数据库或调用任何的业务方法.
String username = request.getParameter("username");
String accountId = request.getParameter("accountId");
StringBuffer errors = validateFormField(username, accountId);
//表单验证通过。
if(errors.toString().equals("")){
errors = validateUser(username, accountId);
//用户名和账号验证通过
if(errors.toString().equals("")){
errors = validateComputerStoreNumber(request);
//库存验证通过
if(errors.toString().equals("")){
errors = validateBalance(request, accountId);
}
}
}
if(!errors.toString().equals("")){
request.setAttribute("errors", errors);
request.getRequestDispatcher("/WEB-INF/pages/cash.jsp").forward(request, response);
return;
}
//验证通过执行具体的逻辑操作
computerService.cash(EStoreWebUtils.getShoppingCart(request), username, accountId);
response.sendRedirect(request.getContextPath() + "/success.jsp");
}
//验证余额是否充足
public StringBuffer validateBalance(HttpServletRequest request, String accountId){
StringBuffer errors = new StringBuffer("");
ShoppingCart cart = EStoreWebUtils.getShoppingCart(request);
Account account = accountService.getAccount(Integer.parseInt(accountId));
if(cart.getTotalMoney() > account.getBalance()){
errors.append("余额不足!");
}
return errors;
}
//验证库存是否充足
public StringBuffer validateComputerStoreNumber(HttpServletRequest request){
StringBuffer errors = new StringBuffer("");
ShoppingCart cart = EStoreWebUtils.getShoppingCart(request);
for(ShoppingCartItem sci: cart.getItems()){
int quantity = sci.getQuantity();
int storeNumber = computerService.getComputer(sci.getComputer().getId()).getStoreNumber();
if(quantity > storeNumber){
errors.append(sci.getComputer().getBrand()+sci.getComputer().getModel() + "库存不足<br>");
}
}
return errors;
}
//验证用户名和账号是否匹配
public StringBuffer validateUser(String username, String accountId){
boolean flag = false;
User user = userService.getUserByUserName(username);
if(user != null){
int accountId2 = user.getAccountId();
if(accountId.trim().equals("" + accountId2)){
flag = true;
}
}
StringBuffer errors2 = new StringBuffer("");
if(!flag){
errors2.append("用户名和账号不匹配");
}
return errors2;
}
//验证表单域是否符合基本的规则: 是否为空.
public StringBuffer validateFormField(String username, String accountId){
StringBuffer errors = new StringBuffer("");
if(username == null || username.trim().equals("")){
errors.append("用户名不能为空<br>");
}
if(accountId == null || accountId.trim().equals("")){
errors.append("账号不能为空");
}
return errors;
}
protected void updateItemQuantity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//4. 在 updateItemQuantity 方法中, 获取 quanity, id, 再获取购物车对象, 调用 service 的方法做修改
String idStr = request.getParameter("id");
String quantityStr = request.getParameter("quantity");
ShoppingCart sc = EStoreWebUtils.getShoppingCart(request);
int id = -1;
int quantity = -1;
try {
id = Integer.parseInt(idStr);
quantity = Integer.parseInt(quantityStr);
} catch (Exception e) {}
if(id > 0 && quantity > 0)
computerService.updateItemQuantity(sc, id, quantity);
//5. 传回 JSON 数据: computerNumber:xx, totalMoney
Map<String, Object> result = new HashMap<String, Object>();
result.put("computerNumber", sc.getComputerNumber());
result.put("totalMoney", sc.getTotalMoney());
Gson gson = new Gson();
String jsonStr = gson.toJson(result);
response.setContentType("text/javascript");
response.getWriter().print(jsonStr);
}
protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idStr = request.getParameter("id");
int id = -1;
boolean flag = false;
try {
id = Integer.parseInt(idStr);
} catch (Exception e) {}
ShoppingCart sc = EStoreWebUtils.getShoppingCart(request);
computerService.removeItemFromShoppingCart(sc, id);
if(sc.isEmpty()){
request.getRequestDispatcher("/WEB-INF/pages/emptycart.jsp").forward(request, response);
return;
}
request.getRequestDispatcher("/WEB-INF/pages/cart.jsp").forward(request, response);
}
protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ShoppingCart sCart = EStoreWebUtils.getShoppingCart(request);
computerService.clearShoppingCart(sCart);
request.getRequestDispatcher("/WEB-INF/pages/emptycart.jsp").forward(request, response);
}
//跳转页面
protected void forwardPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String page = request.getParameter("page");
request.getRequestDispatcher("/WEB-INF/pages/" + page + ".jsp").forward(request, response);
}
//添加到购物车
protected void addToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 获取商品的 id
String idStr = request.getParameter("id");
int id = -1;
boolean flag = false;
try {
id = Integer.parseInt(idStr);
} catch (Exception e) {}
if(id > 0){
//2. 获取购物车对象
ShoppingCart sc = EStoreWebUtils.getShoppingCart(request);
//3. 调用 ComputerService 的 addToCart() 方法把商品放到购物车中
flag = computerService.addToCart(id, sc);
}
if(flag){
//4. 直接调用 getComputers() 方法.
getComputers(request, re
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【项目介绍】 基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip 基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip 【备注】 1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载食用体验! 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈!
资源推荐
资源详情
资源评论
收起资源包目录
基于JavaWeb的网上电子商城管理系统源码(MVC开发毕设项目).zip (74个子文件)
项目说明.md 952B
src
com
xmut
estore
servlet
UserServlet.java 1KB
ComputerServlet.java 10KB
utils
ReflectionUtils.java 5KB
web
EStoreWebUtils.java 720B
Page.java 1KB
CriteriaComputer.java 736B
ConnectionContext.java 578B
dao
AccountDAO.java 464B
TradeItemDAO.java 464B
ComputerDAO.java 1KB
Dao.java 2KB
UserDAO.java 228B
impl
BaseDAO.java 3KB
TradeDAOImpl.java 713B
ComputerDAOImpl.java 3KB
TradeItemDAOImpl.java 1KB
UserDAOImpl.java 361B
AccountDAOIml.java 550B
TradeDAO.java 395B
service
ComputerService.java 3KB
UserService.java 2KB
AccountService.java 334B
filter
TranactionFilter.java 2KB
EncodingFilter.java 1KB
exception
DBException.java 349B
domain
Computer.java 2KB
TradeItem.java 1KB
ShoppingCart.java 2KB
Trade.java 1KB
ShoppingCartItem.java 775B
User.java 1KB
Account.java 670B
db
JDBCUtils.java 2KB
screenshot
3.png 85KB
1.png 292KB
4.png 28KB
2.png 56KB
web
WEB-INF
web.xml 304B
WebContent
WEB-INF
pages
emptycart.jsp 1KB
cart.jsp 3KB
computers.jsp 6KB
computer.jsp 2KB
cash.jsp 2KB
trades.jsp 2KB
lib
c3p0-0.9.1.2.jar 596KB
jstl.jar 20KB
mysql-connector-java-5.1.7-bin.jar 693KB
commons-dbutils-1.3.jar 40KB
standard.jar 384KB
gson-2.2.4.jar 186KB
web.xml 2KB
index.jsp 94B
commons
queryCondition.jsp 511B
footer.jsp 536B
header.jsp 2KB
success.jsp 1KB
public
js
bootstrap.min.js 36KB
cartValidate.js 2KB
jquery-3.2.1.min.js 85KB
img
zard.jpg 748KB
1.jpg 258KB
mac.png 695KB
css
bootstrap.min.css 118KB
bootstrapValidator.css 700B
cart.css 151B
index.css 736B
fonts
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.woff2 18KB
error-1.jsp 1KB
META-INF
MANIFEST.MF 36B
共 74 条
- 1
资源评论
北航程序员小C
- 粉丝: 2222
- 资源: 1823
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功