Day15
作者:互联网
servlet包ControlServlet类
private void delBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bookId = request.getParameter("bookId"); if (bookId != null && !"".equals(bookId)) { s.delBook(bookId); request.setAttribute("msg", "删除图书分类成功!"); request.getRequestDispatcher("/message.jsp").forward(request, response); return; } System.out.println("是不是空"); response.sendRedirect(request.getContextPath() + "/error.jsp"); } //编辑分类:更新内容 private void editCategory(HttpServletRequest request, HttpServletResponse response) { try { Category category = new Category(); BeanUtils.populate(category, request.getParameterMap()); System.out.println("id" + category.getId()); s.editCategory(category); request.setAttribute("msg", "修改分类成功!"); request.getRequestDispatcher("/message.jsp").forward(request, response); } catch (Exception e) { throw new RuntimeException(e); } } //编辑分类: 回显数据 private void editCategoryView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String categoryId = request.getParameter("categoryId"); Category c = s.findCategoryById(categoryId); request.setAttribute("c", c); request.getRequestDispatcher("/edit.jsp").forward(request, response); } //删除分类 private void delCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String categoryId = request.getParameter("categoryId"); if (categoryId != null && !"".equals(categoryId)) { s.delCategory(categoryId); request.setAttribute("msg", "删除图书分类成功!"); request.getRequestDispatcher("/message.jsp").forward(request, response); return; } System.out.println("是不是空"); response.sendRedirect(request.getContextPath() + "/error.jsp"); } //分页查询图书 private void showBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pagenum = request.getParameter("pagenum"); List<Category> cs = s.findAllCategory(); request.setAttribute("cs", cs); Page page = s.findAllBook(pagenum); page.setUrl("servlet/ControlServlet?op=showBook"); request.setAttribute("page", page); request.getRequestDispatcher("/bookList.jsp") .forward(request, response); } //添加图书 private void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { request.setAttribute("msg", "亲,您的表单填写有误,请检查!"); request.getRequestDispatcher("/message.jsp").forward(request, response); return; } DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { String fieldName = item.getFieldName(); String fieldValue = item.getString(request .getCharacterEncoding()); BeanUtils.setProperty(book, fieldName, fieldValue); } else { String fieldName = item.getName(); if (fieldName != null && !fieldName.trim().equals("")) { fieldName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(fieldName); String storeDirectory = getServletContext() .getRealPath("/images/"); // 路径 String path = SetPhotoPath.makeDir(storeDirectory, fieldName); // String path = "";//如果不需要设置多级目录(打散) 可以放开此句话 book.setPath(path); book.setPhotoName(fieldName); // 上传 item.write(new File(storeDirectory + path + "/" + fieldName)); } } } s.addBook(book); request.setAttribute("msg", "添加图书成功"); request.getRequestDispatcher("/message.jsp").forward(request, response); } catch (Exception e) { throw new RuntimeException(e); } } //带着分类名称去跳转到添加图书界面 private void addBookView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Category> cs = s.findAllCategory(); request.setAttribute("cs", cs); request.getRequestDispatcher("/addBook.jsp").forward(request, response); } //用户登录 private void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); String type = request.getParameter("type"); User u = s.userLogin(username, password, type); if (u != null) { // session 表示会话 cookie HttpSession session = request.getSession(); session.setAttribute("u", u); if ("管理员".equals(u.getType())) { response.sendRedirect(request.getContextPath() + "/main.jsp"); return; } else { response.sendRedirect(request.getContextPath() + "/index.jsp"); return; } } else { request.setAttribute("msg", "登录有误!"); request.getRequestDispatcher("/message.jsp").forward(request, response); } } //用户注册 private void userRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user; try { user = new User(); BeanUtils.populate(user, request.getParameterMap()); } catch (Exception e) { throw new RuntimeException(e); } System.out.println("username:" + user.getUsername()); s.userRegister(user); request.setAttribute("msg", "用户注册成功!"); request.getRequestDispatcher("/message.jsp").forward(request, response); } //查询所有分类 private void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Category> cs = s.findAllCategory(); request.setAttribute("cs", cs); request.getRequestDispatcher("/categoryList.jsp").forward(request, response); } //添加分类 private void addCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Category category = new Category(); String name = request.getParameter("name"); String description = request.getParameter("description"); category.setName(name); category.setDescription(description); System.out.println("分类" + category); System.out.println("分类名称:" + category.getName()); s.addCategory(category); request.setAttribute("msg", "添加分类成功!"); request.getRequestDispatcher("/message.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }View Code
exception包AddBookException类
public class AddBookException extends RuntimeException { public AddBookException() { // TODO Auto-generated constructor stub } public AddBookException(String message) { super(message); // TODO Auto-generated constructor stub } public AddBookException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public AddBookException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } }View Code
标签:String,request,void,setAttribute,Day15,jsp,response 来源: https://www.cnblogs.com/headl-voi/p/15823679.html