Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之圖片展覽館管理系統(tǒng)的實(shí)現(xiàn)
一、項(xiàng)目運(yùn)行
環(huán)境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
項(xiàng)目技術(shù):
Springboot+ SpringMVC + JPA+ Jsp + Html+ JavaScript + JQuery + Ajax + maven等等






訂單服務(wù):
@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");// 得到傳過來的請(qǐng)求
// 生成訂單
if (op.equals("genOrder")) {
genOrder(req, resp);
}
// 查看訂單
if (op.equals("findAllOrders")) {
findAllOrders(req, resp);
}
// 管理員查看訂單
if (op.equals("findOrders")) {
findOrders(req, resp);
}
// 發(fā)貨
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) {
resp.getWriter().print("<script>alert('該訂單下有訂單子項(xiàng)不可刪除,請(qǐng)先刪除訂單子項(xiàng)!')</script>");
resp.getWriter().print("<script>location.href='../admin/managerOrder.jsp'</script>");
return;
}
try {
service.deleteByNumber(orderNumber);
resp.getWriter().print("<script>alert('刪除成功!')</script>");
resp.sendRedirect("../admin/managerOrder.jsp");
}catch (Exception e) {
resp.getWriter().print("<script>alert('該訂單下有訂單子項(xiàng)不可刪除,請(qǐng)先刪除訂單子項(xiàng)!')</script>");
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 {
// 取出購(gòu)物車信息
// 取出購(gòu)物項(xiàng)信息
HttpSession session = req.getSession();
Cart cart = (Cart) session.getAttribute("cart");
User user = (User) session.getAttribute("user");
if (cart == null) {
session.setAttribute("message", "會(huì)話已經(jīng)結(jié)束!");
req.getRequestDispatcher("../message.jsp").forward(req, resp);
return;
}
//先判斷庫(kù)存夠不夠!
Book book = cart.getBook();
String book_id = book.getBook_id();
Book findBookById = clientService.findBookById(book_id);
if(findBookById.getBook_kunumber()-cart.getTotalQuantity()<0) {
session.setAttribute("message", "庫(kù)存不足,無法購(gòu)買!");
session.removeAttribute("cart");
req.getRequestDispatcher("../message.jsp").forward(req, resp);
return;
}
Order order = new Order();
order.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
String ordernum = genOrdernum();
order.setOrdernum(ordernum);
order.setQuantity(cart.getTotalQuantity());
order.setMoney(cart.getTotalMoney());
order.setUser(user);
// 訂單項(xiàng)
List<Orderitem> oItems = new ArrayList<Orderitem>();
for (Map.Entry<String, CartItem> me : cart.getItmes().entrySet()) {
CartItem cItem = me.getValue();
Orderitem oItem = new Orderitem();
oItem.setId(genOrdernum());
oItem.setBook(cItem.getBook());
oItem.setPrice(cItem.getMoney());
oItem.setQuantity(cItem.getQuantity());
oItem.setOrdernum(ordernum);
oItems.add(oItem);
}
// 建立訂單項(xiàng)和訂單的關(guān)系
order.setItems(oItems);
service.genOrder(order);
req.setAttribute("order", order);
req.getSession().removeAttribute("cart");
req.removeAttribute("cart");
req.getRequestDispatcher("/order.jsp").forward(req, resp);
}
// 生成訂單號(hào)
private String genOrdernum() {
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String s1 = df.format(now);
return s1 + System.nanoTime();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
管理員登錄服務(wù):
@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");// 得到傳過來的請(qǐng)求
// 管理員登陸
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);
}
// 文學(xué)藝術(shù)類書籍列表
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")) {
editCategoryUI(req, resp);
}
// 修改書籍分類信息
if (op.equals("editCategory")) {
editCategory(req, resp);
}
// 刪除書籍分類
if (op.equals("delCategory")) {
delCategory(req, resp);
}
// 用戶信息管理
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);
}
private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if(username==null || username=="") {
req.setAttribute("message", "請(qǐng)?zhí)顚懹脩裘?);
req.getRequestDispatcher("/admin/404.jsp").forward(req, resp);
return;
}
if(password==null || password=="") {
req.setAttribute("message", "請(qǐng)?zhí)顚懨艽a");
req.getRequestDispatcher("/admin/404.jsp").forward(req, resp);
return;
}
HttpSession session = req.getSession();
Administrator admin = service.login(username, password);
if (admin.getUsername() != null && admin.getUsername() != "") {
req.setAttribute("message", "登陸成功");
session.setAttribute("admin", admin);
req.getRequestDispatcher("/admin/message.jsp").forward(req, resp);
} else {
req.setAttribute("message", "賬號(hào)或密碼錯(cuò)誤!");
req.getRequestDispatcher("/admin/404.jsp").forward(req, resp);
}
}
private void managerInformation(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String username = req.getParameter("username");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String tel = req.getParameter("tel");
service.managerInformation(username, name, sex, tel);
HttpSession session = req.getSession();
Administrator admin = (Administrator) session.getAttribute("admin");
admin.setName(name);
admin.setSex(sex);
admin.setTel(tel);
session.setAttribute("admin", admin);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
//修改管理員密碼
private void managerPassword(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(!password.equals(repassword)) {
resp.getWriter().print("<script>alert('兩次密碼不一致')</script>");
return;
}
Administrator admin = service.findAdminByUsername(username);
if(!admin.getPassword().equals(oldPassword)) {
resp.getWriter().print("<script>alert('舊密碼填寫錯(cuò)誤!')</script>");
return;
}
service.managerPassword(username, password);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
private void layout(HttpServletRequest req, HttpServletResponse resp) {
try {
HttpSession session = req.getSession();
session.removeAttribute("admin");
resp.sendRedirect("../admin/managerLogin.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
private void addBookUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Category> categorys = service.findAllCategory();
req.setAttribute("cs", categorys);
req.getRequestDispatcher("/admin/addBook.jsp").forward(req, resp);
}
private void addBook(HttpServletRequest req, HttpServletResponse resp) throws FileUploadException, IOException {
// 判斷表單是不是multipart/form-data類型
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if (!isMultipart) {
throw new RuntimeException("表單上傳類型有誤!!");
}
// 創(chuàng)建工廠用來 解析請(qǐng)求內(nèi)容
DiskFileItemFactory factory = new DiskFileItemFactory();
// 構(gòu)建核心類對(duì)象
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()) {
// 普通字段:把數(shù)據(jù)分裝到book對(duì)象中
processFormField(item, req, book);
} else {
// 上傳字段:上傳
processUplodFiled(item, req, book);
}
}
// 把書籍信息保存到數(shù)據(jù)庫(kù)中
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.獲取存放文件的真實(shí)目錄
// String dirImages = getServletContext().getRealPath("/images");
String dirImages =req.getServletContext().getRealPath("/")+"images/";
// String dirImages = getServletContext().getRealPath("/img");
// 02. 通過io存文件
// 03. 生成文件名 (用戶上傳圖片, 圖片名可能重復(fù))
String FieldName = item.getFieldName();// 輸入框的name值
String name = item.getName();
String fileType = name.substring(name.lastIndexOf(".") + 1);
String fileName = UUID.randomUUID().toString();// 生成用不重復(fù)的文件名
// 生成文件夾名
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對(duì)象中的路徑
// String path = "books";// 存放到book對(duì)象中
String filename = fileName + "." + fileType;
// fileDir:圖片最終存在于fileDir
File fileDir = new File(dirImages, path + File.separator + filename);
// InputStream inputStream = item.getInputStream(); 從請(qǐng)求 對(duì)象中 通過流的方式
// 把 上傳的文件加載到 內(nèi)存中 構(gòu)建輸出流
File parentDir = new File(dirImages, path);// 父目錄
if (!parentDir.exists()) {
parentDir.mkdirs();
}
book.setFilename(filename);
book.setPath(path);
InputStream inputStream = item.getInputStream();
FileOutputStream fos = new FileOutputStream(fileDir);
int len = 0;
while ((len = inputStream.read()) != -1) {
fos.write(len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 把Fielditem中的數(shù)據(jù)封裝到book中
private void processFormField(FileItem item, HttpServletRequest req, Book book) {
try {
// item每一個(gè)item對(duì)象代表一個(gè)輸入框
// 01. 獲取input輸入框的 name 的值 根據(jù)規(guī)范 輸入框 的name的取值, 與 javabean 中的 屬性名一致
String inputName = item.getFieldName();
String inputValue = item.getString("UTF-8");
if (inputName.equals("categoryid")) {// 分類單獨(dú)處理
// 獲取到選擇的分類的id,根據(jù)這個(gè)id取到分類的信息
String categoryid = item.getString();
System.out.println("categoryid=" + categoryid);
Category category = service.findCategoryById(categoryid);
System.out.println(category);
book.setCategory(category);
} else {
BeanUtils.setProperty(book, inputName, inputValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加書籍分類
private void addCategory(HttpServletRequest req, HttpServletResponse resp) {
try {
Category category = new Category();
BeanUtils.populate(category, req.getParameterMap());
String categoryName = category.getCategory_name();
if(StringUtils.isNullOrEmpty(categoryName)) {
resp.getWriter().print("<script>alert('請(qǐng)?zhí)顚懛诸惷Q')</script>");
resp.getWriter().print("<script>location.href='../admin/addCategory.jsp'</script>");
return;
}
service.addCategory(category);// 調(diào)用添加方法
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>添加成功!</div>");
} catch (Exception e) {
e.printStackTrace();
}
}
private void getCategoryBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Book> books = service.getCategoryBook(req.getParameter("cid"));// 文學(xué)藝術(shù)類書籍
List<Category> categoryList = service.findAllCategory();// 分類
req.setAttribute("books", books);
req.setAttribute("categoryList", categoryList);
req.setAttribute("currentName", "");
req.getRequestDispatcher("/admin/booksList.jsp").forward(req, resp);
}
private void editBookUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String book_id = req.getParameter("book_id");
Book book = findBookById(book_id);
List<Category> category = service.findAllCategory();
HashMap map = new HashMap();
map.put("book", book);
map.put("category", category);
req.setAttribute("map", map);
req.getRequestDispatcher("/admin/editBook.jsp").forward(req, resp);
}
private void editBook(HttpServletRequest req, HttpServletResponse resp) throws FileUploadException, IOException {
String book_id = req.getParameter("book_id");
String book_name = req.getParameter("book_name");
String book_author = req.getParameter("book_author");
String book_press = req.getParameter("book_press");
String book_desc = req.getParameter("book_desc");
double book_price = Double.parseDouble(req.getParameter("book_price"));
String book_kunumber = req.getParameter("book_kunumber");
String categoryId = req.getParameter("categoryid");
Category findCategoryById = service.findCategoryById(categoryId);
service.editBook(book_id, book_name, book_author, book_press, book_desc, book_price, book_kunumber,findCategoryById);
resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
+ "/img/duigou.png'/>修改成功!</div>");
}
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(StringUtils.isNullOrEmpty(categoryName)) {
resp.getWriter().print("<script>alert('請(qǐng)?zhí)顚懛诸惷Q')</script>");
resp.getWriter().print("<script>location.href='../admin/editCategory.jsp'</script>");
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);
}
}
收藏夾業(yè)務(wù)處理:
@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");// 得到傳過來的請(qǐng)求
if (op != null && !op.equals("")) {
// 登錄
if (op.equals("login")) {
login(req, resp);
}
// 注銷
if (op.equals("layout")) {
layout(req, resp);
}
// 注冊(cè)
if (op.equals("register")) {
register(req, resp);
}
// 文學(xué)藝術(shù)類書籍列表
if (op.equals("category")) {
getCategoryBook(req, resp);
}
// 個(gè)人信息修改
if (op.equals("personInformation")) {
personInformation(req, resp);
}
// 修改密碼
if (op.equals("personPassword")) {
personPassword(req, resp);
}
// 搜索框
if (op.equals("search")) {
search(req, resp);
}
// 詳情頁(yè)面
if (op.equals("particulars")) {
particulars(req, resp);
}
// 添加購(gòu)物車
if (op.equals("addCart")) {
addCart(req, resp);
}
// 刪除購(gòu)物車中的購(gòu)物項(xiàng)
if (op.equals("delItem")) {
delItem(req, resp);
}
// 修改購(gòu)物項(xiàng)數(shù)量
if (op.equals("changeNum")) {
changeNum(req, resp);
}
// 添加收藏夾
if (op.equals("addfavorite")) {
addfavorite(req, resp);
}
// 顯示收藏夾
if (op.equals("showfavorite")) {
showfavorite(req, resp);
}
// 刪除收藏夾
if (op.equals("delFavorite")) {
delFavorite(req, resp);
}
// 刪除收藏夾
if (op.equals("buyNow")) {
buNow(req, resp);
}
}
}
private void delFavorite(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String book_id = req.getParameter("book_id");
service.delFavorite(book_id);
HttpSession session = req.getSession();
List<Favorite> lists = (List<Favorite>) session.getAttribute("favorite");
Iterator<Favorite> iterator = lists.iterator();
while (iterator.hasNext()) {
Favorite favorite = iterator.next();
if (book_id.equals(favorite.getBook().getBook_id())) {
iterator.remove();// 使用迭代器的刪除方法刪除
}
}
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");
// 取出購(gòu)物車
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", "用戶名或密碼錯(cuò)誤,請(qǐng)重新登錄");
req.getRequestDispatcher("/message.jsp").forward(req, resp);
}
}
private void layout(HttpServletRequest req, HttpServletResponse resp) {
try {
HttpSession session = req.getSession();
session.removeAttribute("user");// 獲取session對(duì)象,從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");
String tel = req.getParameter("tel");
String address = req.getParameter("address");
boolean isExist = false;// 判斷是否存在該用戶
if (!username.equals("") && !password.equals("")) {
isExist = service.register(username, password, name, sex, tel, address);
if (isExist == true) {
resp.getWriter().print("<script>alert('該用戶已經(jīng)注冊(cè),請(qǐng)直接登錄')</script>");
resp.getWriter().print("<script>location.href='../client/ClientServlet?op=category'</script>");
} else {
resp.getWriter().write("注冊(cè)成功!");
resp.getWriter().print("<script>location.href='../client/ClientServlet?op=category'</script>");
}
}else {
resp.getWriter().print("<script>alert('請(qǐng)?zhí)顚戀~號(hào)或密碼')</script>");
resp.getWriter().print("<script>location.href='../client/ClientServlet?op=category'</script>");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void getCategoryBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Book> books = service.getCategoryBook(req.getParameter("cid"));// 文學(xué)藝術(shù)類書籍
req.setAttribute("books", books);
List<Category> categoryList= managerService.findAllCategory();
req.setAttribute("categoryList", categoryList);
req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
}
private void personInformation(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
String name = req.getParameter("name");
String sex = req.getParameter("sex");
String tel = req.getParameter("tel");
String address = req.getParameter("address");
service.personInformation(username, name, sex, tel, address);
User user = (User) req.getSession().getAttribute("user");
user.setUsername(username);
user.setName(name);
user.setSex(sex);
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)) {
resp.getWriter().print("<script>alert('兩次密碼不一致')</script>");
}
User user = service.findUserByUserName(username);
if(user==null) {
resp.getWriter().print("<script>alert('會(huì)話失效或未登錄,請(qǐng)重新登錄!')</script>");
return;
}
if(!user.getPassword().equals(oldPassword)) {
resp.getWriter().print("<script>alert('舊密碼輸入錯(cuò)誤!')</script>");
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", "購(gòu)物車?yán)镉猩唐?請(qǐng)支付之后再來添加!");
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", "購(gòu)物車?yán)镉猩唐?請(qǐng)支付之后再來添加!");
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);
}
}
到此這篇關(guān)于Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之圖片展覽館管理系統(tǒng)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java 圖片展覽館管理系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之工作管理系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之財(cái)務(wù)預(yù)算管理系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之在線高中考試系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之平行志愿管理系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之教室預(yù)訂管理系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之共享租車信息管理系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之寵物醫(yī)院與商城一體的系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之生活旅行分享平臺(tái)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之健身俱樂部管理系統(tǒng)的實(shí)現(xiàn)
相關(guān)文章
全面解析SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理
這篇文章主要介紹了全面解析SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下2017-05-05
java前后端傳值,參數(shù)有集合類型的數(shù)據(jù)時(shí)的兩種操作方式
這篇文章主要介紹了java前后端傳值,參數(shù)有集合類型的數(shù)據(jù)時(shí)的兩種操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java數(shù)據(jù)庫(kù)操作庫(kù)DButils類的使用方法與實(shí)例詳解
這篇文章主要介紹了JDBC數(shù)據(jù)庫(kù)操作庫(kù)DButils類的使用方法詳解,需要的朋友可以參考下2020-02-02
Java和MySQL數(shù)據(jù)庫(kù)中關(guān)于小數(shù)的保存問題詳析
在Java和MySQL中小數(shù)的精度可能會(huì)受到限制,如float類型的小數(shù)只能精確到6-7位,double類型也只能精確到15-16位,這篇文章主要給大家介紹了關(guān)于Java和MySQL數(shù)據(jù)庫(kù)中關(guān)于小數(shù)的保存問題,需要的朋友可以參考下2024-01-01
如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)
這篇文章主要給大家介紹了關(guān)于如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-12-12

