基于javaweb+mysql的jsp+servlet网上书城+后台管理系统(java+jsp+servlert+mysql+ajax)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet网上书城+后台管理系统(java+jsp+servlert+mysql+ajax)
一、项目简述(附带IW文档)
功能: 前台: * 用户模块 * 分类模块 * 图书模块 * 购物车模块 * 订单模块
后台: * 管理员模块 * 分类管理模块 * 图书管理模块 * 订单模块
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。
int index = url.lastIndexOf("&pc=");
if(index != -1) {
url = url.substring(0, index);
}
return url;
}
/**
* 查看所有订单
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findAll(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Order> pb = orderService.findAll(pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/adminjsps/admin/order/list.jsp";
}
/**
* 按状态查询
* @param req
* @param resp
return;
}
// 校验图片的尺寸
// 保存上传的图片,把图片new成图片对象:Image、Icon、ImageIcon、BufferedImage、ImageIO
/*
* 保存图片:
* 1. 获取真实路径
*/
savepath = this.getServletContext().getRealPath("/book_img");
/*
* 2. 创建目标文件
*/
destFile = new File(savepath, filename);
/*
* 3. 保存文件
*/
try {
fileItem.write(destFile);//它会把临时文件重定向到指定的路径,再删除临时文件
} catch (Exception e) {
throw new RuntimeException(e);
}
// 校验尺寸
// 1. 使用文件路径创建ImageIcon
icon = new ImageIcon(destFile.getAbsolutePath());
// 2. 通过ImageIcon得到Image对象
image = icon.getImage();
// 3. 获取宽高来进行校验
if(image.getWidth(null) > 350 || image.getHeight(null) > 350) {
error("您上传的图片尺寸超出了350*350!", request, response);
//destFile.delete();//删除图片
return;
}
// 把图片的路径设置给book对象
book.setImage_b("book_img/" + filename);
// 调用service完成保存
book.setBid(CommonUtils.uuid());
BookService bookService = new BookService();
bookService.add(book);
// 保存成功信息转发到msg.jsp
req.setAttribute("code", "success");
req.setAttribute("msg", "恭喜,支付成功!");
return "f:/jsps/msg.jsp";
} else if(r9_BType.equals("2")) {
resp.getWriter().print("success");
}
}
return null;
}
/**
* 取消订单
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String cancel(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String oid = req.getParameter("oid");
/*
* 校验订单状态
*/
int status = orderService.findStatus(oid);
if(status != 1) {
req.setAttribute("code", "error");
req.setAttribute("msg", "状态不对,不能取消!");
return "f:/jsps/msg.jsp";
}
orderService.updateStatus(oid, 5);//设置状态为取消!
req.setAttribute("code", "success");
req.setAttribute("msg", "您的订单已取消,您不后悔吗!");
return "f:/jsps/msg.jsp";
}
/**
* 确认收货
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String confirm(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String oid = req.getParameter("oid");
/*
* 校验订单状态
*/
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 按作者查
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByAuthor(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String author = req.getParameter("author");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByAuthor(author, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 按出版社查询
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
PageBean<Book> pb = bookService.findByCombination(criteria, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/adminjsps/admin/book/list.jsp";
}
}
package cn.itcast.goods.admin.category.web.servlet;
public class AdminCategoryServlet extends BaseServlet {
private CategoryService categoryService = new CategoryService();
private BookService bookService = new BookService();
/**
* 查询所有分类
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findAll(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("parents", categoryService.findAll());
return "f:/adminjsps/admin/category/list.jsp";
}
/**
* 添加一级分类
* @param req
* @param resp
* @return
* @throws ServletException
public class AdminAddBookServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
/*
* 1. commons-fileupload的上传三步
*/
// 创建工具
FileItemFactory factory = new DiskFileItemFactory();
/*
* 2. 创建解析器对象
*/
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setFileSizeMax(80 * 1024);//设置单个上传的文件上限为80KB
/*
* 3. 解析request得到List<FileItem>
*/
List<FileItem> fileItemList = null;
try {
fileItemList = sfu.parseRequest(request);
} catch (FileUploadException e) {
// 如果出现这个异步,说明单个文件超出了80KB
error("上传的文件超出了80KB", request, response);
return;
}
/*
* 4. 把List<FileItem>封装到Book对象中
* 4.1 首先把“普通表单字段”放到一个Map中,再把Map转换成Book和Category对象,再建立两者的关系
*/
Map<String,Object> map = new HashMap<String,Object>();
for(FileItem fileItem : fileItemList) {
if(fileItem.isFormField()) {//如果是普通表单字段
map.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
}
}
Book book = CommonUtils.toBean(map, Book.class);//把Map中大部分数据封装到Book对象中
cartItemService.batchDelete(cartItemIds);
/*
* 5. 保存订单,转发到ordersucc.jsp
*/
req.setAttribute("order", order);
return "f:/jsps/order/ordersucc.jsp";
}
/**
* 我的订单
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String myOrders(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 从当前session中获取User
*/
User user = (User)req.getSession().getAttribute("sessionUser");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Order> pb = orderService.myOrders(user.getUid(), pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/order/list.jsp";
}
}
package cn.itcast.goods.user.web.servlet;
return "f:/adminjsps/msg.jsp";
}
/**
* 加载图书
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String load(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 获取bid,得到Book对象,保存之
*/
String bid = req.getParameter("bid");
Book book = bookService.load(bid);
req.setAttribute("book", book);
/*
* 2. 获取所有一级分类,保存之
*/
req.setAttribute("parents", categoryService.findParents());
/*
* 3. 获取当前图书所属的一级分类下所有2级分类
*/
String pid = book.getCategory().getParent().getCid();
req.setAttribute("children", categoryService.findChildren(pid));
/*
* 4. 转发到desc.jsp显示
*/
return "f:/adminjsps/admin/book/desc.jsp";
}
/**
* 添加图书:第一步
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String addPre(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByCategory(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String cid = req.getParameter("cid");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByCategory(cid, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/adminjsps/admin/book/list.jsp";
}
/**
* 按作者查
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByAuthor(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
Book criteria = CommonUtils.toBean(req.getParameterMap(), Book.class);
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByCombination(criteria, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
}
package cn.itcast.goods.admin.web.servlet;
public class AdminOrderServlet extends BaseServlet {
private OrderService orderService = new OrderService();
/**
* 获取当前页码
* @param req
String author = req.getParameter("author");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByAuthor(author, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 按出版社查询
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByPress(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String press = req.getParameter("press");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByPress(press, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
fileItem.write(destFile);//它会把临时文件重定向到指定的路径,再删除临时文件
} catch (Exception e) {
throw new RuntimeException(e);
}
// 校验尺寸
// 1. 使用文件路径创建ImageIcon
icon = new ImageIcon(destFile.getAbsolutePath());
// 2. 通过ImageIcon得到Image对象
image = icon.getImage();
// 3. 获取宽高来进行校验
if(image.getWidth(null) > 350 || image.getHeight(null) > 350) {
error("您上传的图片尺寸超出了350*350!", request, response);
//destFile.delete();//删除图片
return;
}
// 把图片的路径设置给book对象
book.setImage_b("book_img/" + filename);
// 调用service完成保存
book.setBid(CommonUtils.uuid());
BookService bookService = new BookService();
bookService.add(book);
// 保存成功信息转发到msg.jsp
request.setAttribute("msg", "添加图书成功!");
request.getRequestDispatcher("/adminjsps/msg.jsp").forward(request, response);
}
/*
* 保存错误信息,转发到add.jsp
*/
private void error(String msg, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("msg", msg);
request.setAttribute("parents", new CategoryService().findParents());//所有一级分类
request.getRequestDispatcher("/adminjsps/admin/book/add.jsp").
* @return
* @throws ServletException
* @throws IOException
*/
public String batchDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 获取cartItemIds参数
* 2. 调用service方法完成工作
* 3. 返回到list.jsp
*/
String cartItemIds = req.getParameter("cartItemIds");
cartItemService.batchDelete(cartItemIds);
return myCart(req, resp);
}
/**
* 添加购物车条目
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String add(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 封装表单数据到CartItem(bid, quantity)
*/
Map map = req.getParameterMap();
CartItem cartItem = CommonUtils.toBean(map, CartItem.class);
Book book = CommonUtils.toBean(map, Book.class);
User user = (User)req.getSession().getAttribute("sessionUser");
cartItem.setBook(book);
cartItem.setUser(user);
/*
* 2. 调用service完成添加
*/
cartItemService.add(cartItem);
/*
* 3. 查询出当前用户的所有条目,转发到list.jsp显示
*/
return myCart(req, resp);
}
/**
* 获取当前页码
* @param req
* @return
*/
private int getPc(HttpServletRequest req) {
int pc = 1;
String param = req.getParameter("pc");
if(param != null && !param.trim().isEmpty()) {
try {
pc = Integer.parseInt(param);
} catch(RuntimeException e) {}
}
return pc;
}
/**
* 截取url,页面中的分页导航中需要使用它做为超链接的目标!
* @param req
* @return
*/
/*
* http://localhost:8080/goods/BookServlet?methed=findByCategory&cid=xxx&pc=3
* /goods/BookServlet + methed=findByCategory&cid=xxx&pc=3
*/
private String getUrl(HttpServletRequest req) {
String url = req.getRequestURI() + "?" + req.getQueryString();
/*
* 如果url中存在pc参数,截取掉,如果不存在那就不用截取。
*/
int index = url.lastIndexOf("&pc=");
if(index != -1) {
url = url.substring(0, index);
}
return url;
*/
Book book = bookService.load(bid);
String savepath = this.getServletContext().getRealPath("/");//获取真实的路径
//new File(savepath, book.getImage_w()).delete();//删除文件
//new File(savepath, book.getImage_b()).delete();//删除文件
bookService.delete(bid);//删除数据库的记录
req.setAttribute("msg", "删除图书成功!");
return "f:/adminjsps/msg.jsp";
}
/**
* 修改图书
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String edit(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 把表单数据封装到Book对象中
* 2. 封装cid到Category中
* 3. 把Category赋给Book
* 4. 调用service完成工作
* 5. 保存成功信息,转发到msg.jsp
*/
Map map = req.getParameterMap();
Book book = CommonUtils.toBean(map, Book.class);
Category category = CommonUtils.toBean(map, Category.class);
book.setCategory(category);
bookService.edit(book);
req.setAttribute("msg", "修改图书成功!");
return "f:/adminjsps/msg.jsp";
}
/**
* @throws IOException
*/
public String ajaxValidateVerifyCode(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 获取输入框中的验证码
*/
String verifyCode = req.getParameter("verifyCode");
/*
* 2. 获取图片上真实的校验码
*/
String vcode = (String) req.getSession().getAttribute("vCode");
/*
* 3. 进行忽略大小写比较,得到结果
*/
boolean b = verifyCode.equalsIgnoreCase(vcode);
/*
* 4. 发送给客户端
*/
resp.getWriter().print(b);
return null;
}
/**
* 注册功能
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String regist(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 封装表单数据到User对象
*/
User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);
/*
* 2. 校验之, 如果校验失败,保存错误信息,返回到regist.jsp显示
*/
Map<String,String> errors = validateRegist(formUser, req.getSession());
if(errors.size() > 0) {
}
}
}
/*
* 登录校验方法,内容等你自己来完成
*/
private Map<String,String> validateLogin(User formUser, HttpSession session) {
Map<String,String> errors = new HashMap<String,String>();
return errors;
}
}
package cn.itcast.goods.admin.book.web.servlet;
public class AdminBookServlet extends BaseServlet {
private BookService bookService = new BookService();
private CategoryService categoryService = new CategoryService();
/**
* 删除图书
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String delete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String bid = req.getParameter("bid");
/*
* 删除图片
*/
Book book = bookService.load(bid);
PageBean<Book> pb = bookService.findByCategory(cid, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/adminjsps/admin/book/list.jsp";
}
/**
* 按作者查
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByAuthor(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String author = req.getParameter("author");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByAuthor(author, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/adminjsps/admin/book/list.jsp";
}
/**
* 按出版社查询
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByPress(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
*/
String bname = req.getParameter("bname");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByBname(bname, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 多条件组合查询
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByCombination(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
Book criteria = CommonUtils.toBean(req.getParameterMap(), Book.class);
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByCombination(criteria, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
categoryService.delete(cid);
return findAll(req, resp);
}
}
/**
* 删除2级分类
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String deleteChild(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 获取cid,即2级分类id
* 2. 获取该分类下的图书个数
* 3. 如果大于零,保存错误信息,转发到msg.jsp
* 4. 如果等于零,删除之,返回到list.jsp
*/
String cid = req.getParameter("cid");
int cnt = bookService.findBookCountByCategory(cid);
if(cnt > 0) {
req.setAttribute("msg", "该分类下还存在图书,不能删除!");
return "f:/adminjsps/msg.jsp";
} else {
categoryService.delete(cid);
return findAll(req, resp);
}
}
}
package cn.itcast.goods.admin.book.web.servlet;
* @throws ServletException
* @throws IOException
*/
public String findByCategory(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String cid = req.getParameter("cid");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByCategory(cid, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/adminjsps/admin/book/list.jsp";
}
/**
* 按作者查
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByAuthor(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
public class OrderServlet extends BaseServlet {
private OrderService orderService = new OrderService();
private CartItemService cartItemService = new CartItemService();
/**
* 获取当前页码
* @param req
* @return
*/
private int getPc(HttpServletRequest req) {
int pc = 1;
String param = req.getParameter("pc");
if(param != null && !param.trim().isEmpty()) {
try {
pc = Integer.parseInt(param);
} catch(RuntimeException e) {}
}
return pc;
}
/**
* 截取url,页面中的分页导航中需要使用它做为超链接的目标!
* @param req
* @return
*/
/*
* http://localhost:8080/goods/BookServlet?methed=findByCategory&cid=xxx&pc=3
* /goods/BookServlet + methed=findByCategory&cid=xxx&pc=3
*/
private String getUrl(HttpServletRequest req) {
String url = req.getRequestURI() + "?" + req.getQueryString();
/*
* 如果url中存在pc参数,截取掉,如果不存在那就不用截取。
*/
int index = url.lastIndexOf("&pc=");
if(index != -1) {
url = url.substring(0, index);
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByStatus(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取链接参数:status
*/
int status = Integer.parseInt(req.getParameter("status"));
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Order> pb = orderService.findByStatus(status, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/adminjsps/admin/order/list.jsp";
}
/**
* 查看订单详细信息
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String load(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String oid = req.getParameter("oid");
Order order = orderService.load(oid);
req.setAttribute("order", order);
String btn = req.getParameter("btn");//btn说明了用户点击哪个超链接来访问本方法的
req.setAttribute("btn", btn);
return "/adminjsps/admin/order/desc.jsp";
}
}
}
package cn.itcast.goods.cart.web.servlet;
public class CartItemServlet extends BaseServlet {
private CartItemService cartItemService = new CartItemService();
/**
* 加载多个CartItem
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String loadCartItems(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 获取cartItemIds参数
*/
String cartItemIds = req.getParameter("cartItemIds");
double total = Double.parseDouble(req.getParameter("total"));
/*
* 2. 通过service得到List<CartItem>
*/
}
/**
* 发货功能
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String deliver(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String oid = req.getParameter("oid");
/*
* 校验订单状态
*/
int status = orderService.findStatus(oid);
if(status != 2) {
req.setAttribute("code", "error");
req.setAttribute("msg", "状态不对,不能发货!");
return "f:/adminjsps/msg.jsp";
}
orderService.updateStatus(oid, 3);//设置状态为取消!
req.setAttribute("code", "success");
req.setAttribute("msg", "您的订单已发货,请查看物流,马上确认吧!");
return "f:/adminjsps/msg.jsp";
}
}
package cn.itcast.goods.cart.web.servlet;
* 登录功能
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String login(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 封装表单数据到User
* 2. 校验表单数据
* 3. 使用service查询,得到User
* 4. 查看用户是否存在,如果不存在:
* * 保存错误信息:用户名或密码错误
* * 保存用户数据:为了回显
* * 转发到login.jsp
* 5. 如果存在,查看状态,如果状态为false:
* * 保存错误信息:您没有激活
* * 保存表单数据:为了回显
* * 转发到login.jsp
* 6. 登录成功:
* * 保存当前查询出的user到session中
* * 保存当前用户的名称到cookie中,注意中文需要编码处理。
*/
/*
* 1. 封装表单数据到user
*/
User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);
/*
* 2. 校验
*/
Map<String,String> errors = validateLogin(formUser, req.getSession());
if(errors.size() > 0) {
req.setAttribute("form", formUser);
req.setAttribute("errors", errors);
return "f:/jsps/user/login.jsp";
}
/*
* 3. 调用userService#login()方法
*/
User user = userService.login(formUser);
/*
* 4. 开始判断
*/
if(user == null) {
req.setAttribute("msg", "用户名或密码错误!");
req.setAttribute("user", formUser);
public class AdminCategoryServlet extends BaseServlet {
private CategoryService categoryService = new CategoryService();
private BookService bookService = new BookService();
/**
* 查询所有分类
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findAll(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("parents", categoryService.findAll());
return "f:/adminjsps/admin/category/list.jsp";
}
/**
* 添加一级分类
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String addParent(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 封装表单数据到Category中
* 2. 调用service的add()方法完成添加
* 3. 调用findAll(),返回list.jsp显示所有分类
*/
Category parent = CommonUtils.toBean(req.getParameterMap(), Category.class);
parent.setCid(CommonUtils.uuid());//设置cid
categoryService.add(parent);
return findAll(req, resp);
}