通过servlet转发到另一个页面出现404错误

发布于 2022-09-04 03:02:44 字数 5672 浏览 10 评论 0

图片描述

图片描述

图片描述

web.xml:
图片描述

请问哪里的路径错了??

CatServlet代码如下:

public class CatServlet extends HttpServlet {
    private BaseDao<Cat> dao = new BaseDao<Cat>();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String action = request.getParameter("action");

        if ("initAdd".equals(action)) {
            initAdd(request, response);                 //显示添加页面
        } else if ("add".equals(action)) {
            add(request, response);                     //向数据库中插入一条新数据
        } else if ("edit".equals(action)) {
            edit(request, response);                    //显示修改页面
        } else if ("save".equals(action)) {
            save(request, response);                    //将修改后的数据保存进数据库
        } else if ("view".equals(action)) {
            view(request, response);                    //显示id对应的实体类属性
        } else if ("list".equals(action)) {
            list(request, response);                    //显示所有的实体类对象
        } else if ("delete".equals(action)) {
            delete(request, response);                  //删除id对应的实体类
        } else {
            list(request, response);                    //默认显示所有的实体类对象
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int motherId = Integer.parseInt(request.getParameter("motherId"));  //母亲的id
        String name = request.getParameter("name");
        String description = request.getParameter("description");

        Cat mother = dao.find(Cat.class, motherId);

        //实例化Cat
        Cat cat = new Cat();
        cat.setMother(mother);
        cat.setName(name);
        cat.setDescription("description");
        cat.setCreateDate(new Date());

        dao.create(cat);
        request.setAttribute("msg", "添加'" + cat.getName() + "'成功");
        list(request, response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int motherId = Integer.parseInt(request.getParameter("motherId"));
        String name = request.getParameter("name");
        String description = request.getParameter("description");

        Cat cat = dao.find(Cat.class, id);
        Cat mother = dao.find(Cat.class, motherId);

        cat.setName(name);
        cat.setDescription(description);
        cat.setMother(mother);

        boolean hasLoop = false;
        Cat tmpMother = mother;
        while (tmpMother != null) {
            if (tmpMother.getId().intValue() == cat.getId().intValue()) {
                hasLoop = true;
                break;
            }
            tmpMother = tmpMother.getMother();
        }
        if (!hasLoop) {
            dao.update(cat);
            request.setAttribute("msg", "保存'" + cat.getName() + "'成功");
        } else {
            request.setAttribute("msg", "保存失败!发现循环!");
        }
        list(request, response);
    }

    private void view(HttpServletRequest request, HttpServletResponse response) {
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Cat cat = dao.find(Cat.class, id);
        if (cat != null) {
            List<Cat> catList = dao.list("select c from Cat c where c.mother.id=" + id);
            if (catList.size() > 0) {
                request.setAttribute("msg","无法删除" + cat.getName() + "请先删除子Cat.");
            } else {
                dao.delete(cat);
                request.setAttribute("msg", "删除" + cat.getName() + "成功");
            }
        }
        list(request, response);
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Cat cat = dao.find(Cat.class, id);
        request.setAttribute("cat", cat);
        request.setAttribute("catList", dao.list("from Cat"));  //设置mother时用
        request.getRequestDispatcher("/addCat.jsp").forward(request, response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("catList", dao.list(" from Cat "));    //查询,放到request中
        request.getRequestDispatcher("/listCat.jsp").forward(request, response);
    }

    private void initAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Cat> catList = dao.list("select c from Cat c");
        request.setAttribute("catList", catList);
        request.getRequestDispatcher("/addCat.jsp").forward(request, response);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2022-09-11 03:02:44

tomcat如果访问需要带项目名的话,你这样调用就是会404的,原因是路径不全.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文