java - 通过servlet转发到另一个页面出现404错误
伊谢尔伦
伊谢尔伦 2017-04-18 10:18:18
[Java讨论组]

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);
    }
}
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
怪我咯

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号