通过servlet转发到另一个页面出现404错误
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tomcat如果访问需要带项目名的话,你这样调用就是会404的,原因是路径不全.