基于javaweb+mysql的jsp+servlet网上书店图书商城(前台、后台)(java+jsp+servlet+javabean+mysql+tomcat)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
前台:登录注册、管理个人信息、修改密码、收藏图书、下单购买
后台:管理个人信息、书籍管理、分类管理、图书管理、订单管理、用户管理、添加管理员,销售统计信息
前台
后台
技术框架
JSP Servlet MySQL JDBC Tomcat CSS JavaScript bootstrap jquery
基于javaweb+mysql的JSP+Servlet网上书店图书商城(前台、后台)(java+jsp+servlet+javabean+mysql+tomcat)
user.setTel(tel);
user.setAddress(address);
req.getSession().setAttribute("user", user);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
private void personPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String repassword = req.getParameter("repassword");
String oldPassword = req.getParameter("oldPassword");
if(!repassword.equals(password)) {
}
User user = service.findUserByUserName(username);
if(user==null) {
return;
}
if(!user.getPassword().equals(oldPassword)) {
return;
}
service.personPassword(password, username);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String search = req.getParameter("search");
List<Book> searchmessage = service.search(search);
req.setAttribute("books", searchmessage);
req.setAttribute("name", search==null?"":search);
req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
}
private void particulars(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
req.setAttribute("book", book);
req.getRequestDispatcher("/particulars.jsp").forward(req, resp);
}
@WebServlet("/client/ClientServlet")
public class ClientServlet extends HttpServlet {
private ClientService service = new ClientServiceImpl();
private ManagerService managerService = new ManagerServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String op = req.getParameter("op");// 得到传过来的请求
if (op != null && !op.equals("")) {
// 登录
if (op.equals("login")) {
login(req, resp);
}
// 注销
if (op.equals("layout")) {
layout(req, resp);
}
// 注册
if (op.equals("register")) {
register(req, resp);
}
// 文学艺术类书籍列表
if (op.equals("category")) {
getCategoryBook(req, resp);
}
// 个人信息修改
if (op.equals("personInformation")) {
personInformation(req, resp);
}
// 修改密码
if (op.equals("personPassword")) {
personPassword(req, resp);
}
return;
}
service.personPassword(password, username);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String search = req.getParameter("search");
List<Book> searchmessage = service.search(search);
req.setAttribute("books", searchmessage);
req.setAttribute("name", search==null?"":search);
req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
}
private void particulars(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
req.setAttribute("book", book);
req.getRequestDispatcher("/particulars.jsp").forward(req, resp);
}
// 通过书籍id找到书籍信息
private Book findBookById(String book_id) {
Book book = service.findBookById(book_id);
return book;
}
private void addCart(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
HttpSession session = req.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
cart.addBook(book);
cart.setBook(book);
session.setAttribute("cart", cart);
}else {
session.setAttribute("message", "购物车里有商品,请支付之后再来添加!");
req.getRequestDispatcher("../message.jsp").forward(req, resp);
return;
private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String search = req.getParameter("search");
List<Book> searchmessage = service.search(search);
req.setAttribute("books", searchmessage);
req.setAttribute("name", search==null?"":search);
req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
}
private void particulars(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
req.setAttribute("book", book);
req.getRequestDispatcher("/particulars.jsp").forward(req, resp);
}
// 通过书籍id找到书籍信息
private Book findBookById(String book_id) {
Book book = service.findBookById(book_id);
return book;
}
private void addCart(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
HttpSession session = req.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
cart.addBook(book);
cart.setBook(book);
session.setAttribute("cart", cart);
}else {
session.setAttribute("message", "购物车里有商品,请支付之后再来添加!");
req.getRequestDispatcher("../message.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("../showCart.jsp").forward(req, resp);
}
private void delItem(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String book_id = req.getParameter("book_id");
Cart cart = (Cart) req.getSession().getAttribute("cart");
cart.getItmes().remove(book_id);
req.getSession().removeAttribute("cart");
resp.sendRedirect(req.getContextPath() + "/showCart.jsp");
}
private void buNow(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
Cart cart1 = (Cart) req.getSession().getAttribute("cart");
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String op = req.getParameter("op");// 得到传过来的请求
// 生成订单
if (op.equals("genOrder")) {
genOrder(req, resp);
}
// 查看订单
if (op.equals("findAllOrders")) {
findAllOrders(req, resp);
}
// 管理员查看订单
if (op.equals("findOrders")) {
findOrders(req, resp);
}
// 发货
if (op.equals("faHuo")) {
faHuo(req, resp);
}
//删除订单
if(op.equals("deletes")) {
deletes(req,resp);
}
}
private void deletes(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String orderNumber = req.getParameter("orderNum");
Order findOrderByNum = service.findOrderByNum(orderNumber);
if(findOrderByNum.getItems().size()!=0) {
return;
}
try {
service.deleteByNumber(orderNumber);
resp.sendRedirect("../admin/managerOrder.jsp");
}catch (Exception e) {
return;
}
}
private void faHuo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String ordernum = req.getParameter("ordernum");
service.faHuo(ordernum);
List<Order> orders = service.findOrders();
HttpSession session = req.getSession();
session.setAttribute("orders", orders);
System.out.println(orders);
resp.sendRedirect(req.getContextPath() + "/admin/managerOrder.jsp");
}
}
private void changeNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String num = req.getParameter("num");
String book_id = req.getParameter("book_id");
// 取出购物车
Cart cart = (Cart) req.getSession().getAttribute("cart");
CartItem item = cart.getItmes().get(book_id);
item.setQuantity(Integer.parseInt(num));
resp.sendRedirect(req.getContextPath() + "/showCart.jsp");
}
private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
HttpSession session = req.getSession();
User user = service.login(username, password);
if (user.getUsername() != null && user.getUsername() != "") {
req.setAttribute("message", "登陆成功");
session.setAttribute("user", user);
req.getRequestDispatcher("/message.jsp").forward(req, resp);
} else {
req.setAttribute("message", "用户名或密码错误,请重新登录");
req.getRequestDispatcher("/message.jsp").forward(req, resp);
}
}
private void layout(HttpServletRequest req, HttpServletResponse resp) {
try {
HttpSession session = req.getSession();
session.removeAttribute("user");// 获取session对象,从session中移除登陆信息
resp.sendRedirect("../client/ClientServlet?op=category");
} catch (Exception e) {
e.printStackTrace();
}
}
private void register(HttpServletRequest req, HttpServletResponse resp) {
try {
String username = req.getParameter("username");
String password = req.getParameter("password");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
cart.setBook(book);
session.setAttribute("cart", cart);
}else {
session.setAttribute("message", "购物车里有商品,请支付之后再来添加!");
req.getRequestDispatcher("../message.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("../showCart.jsp").forward(req, resp);
}
private void delItem(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String book_id = req.getParameter("book_id");
Cart cart = (Cart) req.getSession().getAttribute("cart");
cart.getItmes().remove(book_id);
req.getSession().removeAttribute("cart");
resp.sendRedirect(req.getContextPath() + "/showCart.jsp");
}
private void buNow(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
Cart cart1 = (Cart) req.getSession().getAttribute("cart");
if(cart1==null) {
Cart cart = new Cart();
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
cart.addBook(book);
cart.setBook(book);
req.getSession().setAttribute("cart", cart);
}else {
req.getSession().setAttribute("message", "购物车里有商品,请支付之后再来添加!");
req.getRequestDispatcher("../message.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("../showCart.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
private void delBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String book_id = req.getParameter("book_id");
service.delBook(book_id);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>删除成功!</div>");
}
private void categoryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Category> categoryList = service.findAllCategory();
req.setAttribute("categoryList", categoryList);
req.getRequestDispatcher("/admin/categorysList.jsp").forward(req, resp);
}
private void editCategoryUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Category category = service.findCategoryById(req.getParameter("category_id"));
req.setAttribute("category", category);
req.getRequestDispatcher("/admin/editCategory.jsp").forward(req, resp);
}
private void editCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Category category = new Category(req.getParameter("category_id"), req.getParameter("category_name"),
req.getParameter("category_desc"));
String categoryName = category.getCategory_name();
if (categoryName == null || categoryName.trim().equals("")) {
return;
}
service.editCategory(category);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
private void delCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String category_id = req.getParameter("category_id");
service.delCategory(category_id);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>删除成功!</div>");
}
// 通过书籍id找到书籍信息
private Book findBookById(String book_id) {
return service.findBookById(book_id);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
}
}
resp.sendRedirect(req.getContextPath() + "/favorite.jsp");
}
private void showfavorite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
User user = (User) session.getAttribute("user");
List<Favorite> favorites = service.findFavoriteByUserId(user);
session.setAttribute("favorite", favorites);
req.getRequestDispatcher("/favorite.jsp").forward(req, resp);
}
private void addfavorite(HttpServletRequest req, HttpServletResponse resp) throws IOException {
HttpSession session = req.getSession();
User user = (User) session.getAttribute("user");
String user_id = user.getId();
String book_id = req.getParameter("book_id");
boolean isExit = service.findFavorite(user_id, book_id);
if (isExit == false) {
service.addfavorite(user_id, book_id);
}
}
private void changeNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String num = req.getParameter("num");
String book_id = req.getParameter("book_id");
// 取出购物车
Cart cart = (Cart) req.getSession().getAttribute("cart");
CartItem item = cart.getItmes().get(book_id);
item.setQuantity(Integer.parseInt(num));
resp.sendRedirect(req.getContextPath() + "/showCart.jsp");
}
private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
HttpSession session = req.getSession();
User user = service.login(username, password);
if (user.getUsername() != null && user.getUsername() != "") {
req.setAttribute("message", "登陆成功");
session.setAttribute("user", user);
req.getRequestDispatcher("/message.jsp").forward(req, resp);
} else {
req.setAttribute("message", "用户名或密码错误,请重新登录");
// 用户信息管理
if (op.equals("findUsers")) {
findUsers(req, resp);
}
// 添加工作人员
if (op.equals("addAdmin")) {
addAdmin(req, resp);
}
// 书籍销售情况
if (op.equals("sales")) {
sales(req, resp);
}
//搜索
if (op.equals("search")) {
search(req, resp);
}
}
private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("search");
if (name == null) {
name = "";
}
List<Book> findListByBookName = service.findListByBookName(name);
List<Category> categoryList = service.findAllCategory();// 分类
req.setAttribute("currentName", name);
req.setAttribute("books", findListByBookName);
req.setAttribute("categoryList", categoryList);
req.getRequestDispatcher("/admin/booksList.jsp").forward(req, resp);
}
private void sales(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Book> sales = service.sales();
req.setAttribute("sales", sales);
req.getRequestDispatcher("/admin/sales.jsp").forward(req, resp);
}
private void addAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String tel = req.getParameter("tel");
Administrator admin = new Administrator(username, password, name, sex, tel);
service.addAdmin(admin);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>添加成功!</div>");
}
private void findUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<User> list = service.findUsers();
HttpSession session = req.getSession();
session.setAttribute("users", list);
req.getRequestDispatcher("/admin/managerUsers.jsp").forward(req, resp);
// 创建工厂用来 解析请求内容
DiskFileItemFactory factory = new DiskFileItemFactory();
// 构建核心类对象
ServletFileUpload sfu = new ServletFileUpload(factory);
List<FileItem> items = new ArrayList<FileItem>();
items = sfu.parseRequest(req);
Book book = new Book();
for (FileItem item : items) {
if (item.isFormField()) {
// 普通字段:把数据分装到book对象中
processFormField(item, req, book);
} else {
// 上传字段:上传
processUplodFiled(item, req, book);
}
}
// 把书籍信息保存到数据库中
service.addBook(book);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>添加成功!</div>");
}
// 处理文件上传
private void processUplodFiled(FileItem item, HttpServletRequest req, Book book) {
try {
// 存放路径:不要放在web-inf中
// 01.获取存放文件的真实目录
// String dirImages = getServletContext().getRealPath("/images");
String dirImages = req.getServletContext().getRealPath("/") + "images/";
// String dirImages = getServletContext().getRealPath("/img");
// 02. 通过io存文件
// 03. 生成文件名 (用户上传图片, 图片名可能重复)
String FieldName = item.getFieldName();// 输入框的name值
String name = item.getName();
String fileType = name.substring(name.lastIndexOf(".") + 1);
String fileName = UUID.randomUUID().toString();// 生成用不重复的文件名
// 生成文件夹名
Date time = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strTime = simpleDateFormat.format(time);
// path属性filename
String path = strTime + File.separator + "books";// 存放到book对象中的路径
// String path = "books";// 存放到book对象中
if (op.equals("faHuo")) {
faHuo(req, resp);
}
//删除订单
if(op.equals("deletes")) {
deletes(req,resp);
}
}
private void deletes(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String orderNumber = req.getParameter("orderNum");
Order findOrderByNum = service.findOrderByNum(orderNumber);
if(findOrderByNum.getItems().size()!=0) {
return;
}
try {
service.deleteByNumber(orderNumber);
resp.sendRedirect("../admin/managerOrder.jsp");
}catch (Exception e) {
return;
}
}
private void faHuo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String ordernum = req.getParameter("ordernum");
service.faHuo(ordernum);
List<Order> orders = service.findOrders();
HttpSession session = req.getSession();
session.setAttribute("orders", orders);
System.out.println(orders);
resp.sendRedirect(req.getContextPath() + "/admin/managerOrder.jsp");
}
private void findOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Order> orders = service.findOrders();
HttpSession session = req.getSession();
session.setAttribute("orders", orders);
req.getRequestDispatcher("/admin/managerOrder.jsp").forward(req, resp);
}
private void findAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
User user = (User) session.getAttribute("user");
List<Order> orders = service.findUserOrders(user);
req.setAttribute("orders", orders);
req.getRequestDispatcher("/person/personOrder.jsp").forward(req, resp);
}
private void genOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
req.getSession().setAttribute("message", "购物车里有商品,请支付之后再来添加!");
req.getRequestDispatcher("../message.jsp").forward(req, resp);
return;
}
req.getRequestDispatcher("../showCart.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
@WebServlet("/order/OrderServlet")
public class OrderServlet extends HttpServlet {
private OrderService service = new OrderServiceImpl();
private ClientServiceImpl clientService=new ClientServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String op = req.getParameter("op");// 得到传过来的请求
// 生成订单
}
// 添加书籍前先获取所有分类
if (op.equals("addBookUI")) {
addBookUI(req, resp);
}
// 添加书籍
if (op.equals("addBook")) {
try {
addBook(req, resp);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 添加书籍分类
if (op.equals("addCategory")) {
addCategory(req, resp);
}
// 文学艺术类书籍列表
if (op.equals("category")) {
getCategoryBook(req, resp);
}
// 编辑书籍信息前获取书籍的信息回显
if (op.equals("editBookUI")) {
editBookUI(req, resp);
}
// 编辑书籍
if (op.equals("editBook")) {
try {
editBook(req, resp);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 删除书籍
if (op.equals("delBook")) {
delBook(req, resp);
}
// 获取书籍分类列表
if (op.equals("categoryList")) {
categoryList(req, resp);
}
// 获得分类信息
if (op.equals("editCategoryUI")) {
}
// 用户信息管理
if (op.equals("findUsers")) {
findUsers(req, resp);
}
// 添加工作人员
if (op.equals("addAdmin")) {
addAdmin(req, resp);
}
// 书籍销售情况
if (op.equals("sales")) {
sales(req, resp);
}
//搜索
if (op.equals("search")) {
search(req, resp);
}
}
private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("search");
if (name == null) {
name = "";
}
List<Book> findListByBookName = service.findListByBookName(name);
List<Category> categoryList = service.findAllCategory();// 分类
req.setAttribute("currentName", name);
req.setAttribute("books", findListByBookName);
req.setAttribute("categoryList", categoryList);
req.getRequestDispatcher("/admin/booksList.jsp").forward(req, resp);
}
private void sales(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Book> sales = service.sales();
req.setAttribute("sales", sales);
req.getRequestDispatcher("/admin/sales.jsp").forward(req, resp);
}
private void addAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String tel = req.getParameter("tel");
@WebServlet("/admin/ManagerServlet")
public class ManagerServlet extends HttpServlet {
private ManagerService service = new ManagerServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String op = req.getParameter("op");// 得到传过来的请求
// 管理员登陆
if (op.equals("login")) {
login(req, resp);
}
// 修改管理员资料
if (op.equals("managerInformation")) {
managerInformation(req, resp);
}
// 修改管理员登录密码
if (op.equals("managerPassword")) {
managerPassword(req, resp);
}
// 注销
if (op.equals("layout")) {
layout(req, resp);
}
// 添加书籍前先获取所有分类
if (op.equals("addBookUI")) {
addBookUI(req, resp);
}
// 添加书籍
if (op.equals("addBook")) {
try {
addBook(req, resp);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 添加书籍分类
if (op.equals("addCategory")) {
addCategory(req, resp);
}
// 文学艺术类书籍列表
+ "/img/duigou.png'/>修改成功!</div>");
}
private void personPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String repassword = req.getParameter("repassword");
String oldPassword = req.getParameter("oldPassword");
if(!repassword.equals(password)) {
}
User user = service.findUserByUserName(username);
if(user==null) {
return;
}
if(!user.getPassword().equals(oldPassword)) {
return;
}
service.personPassword(password, username);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String search = req.getParameter("search");
List<Book> searchmessage = service.search(search);
req.setAttribute("books", searchmessage);
req.setAttribute("name", search==null?"":search);
req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
}
private void particulars(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
req.setAttribute("book", book);
req.getRequestDispatcher("/particulars.jsp").forward(req, resp);
}
// 通过书籍id找到书籍信息
private Book findBookById(String book_id) {
Book book = service.findBookById(book_id);
items = sfu.parseRequest(req);
Book book = new Book();
for (FileItem item : items) {
if (item.isFormField()) {
// 普通字段:把数据分装到book对象中
processFormField(item, req, book);
} else {
// 上传字段:上传
processUplodFiled(item, req, book);
}
}
// 把书籍信息保存到数据库中
service.addBook(book);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>添加成功!</div>");
}
// 处理文件上传
private void processUplodFiled(FileItem item, HttpServletRequest req, Book book) {
try {
// 存放路径:不要放在web-inf中
// 01.获取存放文件的真实目录
// String dirImages = getServletContext().getRealPath("/images");
String dirImages = req.getServletContext().getRealPath("/") + "images/";
// String dirImages = getServletContext().getRealPath("/img");
// 02. 通过io存文件
// 03. 生成文件名 (用户上传图片, 图片名可能重复)
String FieldName = item.getFieldName();// 输入框的name值
String name = item.getName();
String fileType = name.substring(name.lastIndexOf(".") + 1);
String fileName = UUID.randomUUID().toString();// 生成用不重复的文件名
// 生成文件夹名
Date time = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strTime = simpleDateFormat.format(time);
// path属性filename
String path = strTime + File.separator + "books";// 存放到book对象中的路径
// String path = "books";// 存放到book对象中
String filename = fileName + "." + fileType;
// fileDir:图片最终存在于fileDir
File fileDir = new File(dirImages, path + File.separator + filename);
// InputStream inputStream = item.getInputStream(); 从请求 对象中 通过流的方式
// 把 上传的文件加载到 内存中 构建输出流
File parentDir = new File(dirImages, path);// 父目录