ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day15

2022-01-19 20:04:00  阅读:152  来源: 互联网

标签:String request void setAttribute Day15 jsp response


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有