image.png

解决办法

对于重定向的问题

先获取contextPath虚拟目录,在用拼接字符串的方式进行重定向
这样tomcat的目录配置就不会影响到代码执行

  1. // resp.setStatus(302);
  2. // resp.setHeader("location", "/ServletForward");//此处location不区分大小写
  3. //此处有简化方式进行重定向
  4. //302 和 location 这种固定格式 都不用写了
  5. // resp.sendRedirect("/ServletForward");
  6. // //使用tomcat8加载时,需要修改路径为/tomcatdemo_war/ServletForward
  7. //
  8. String contextPath = req.getContextPath();
  9. resp.sendRedirect(contextPath + "/ServletForward");
  10. //这种写法能够动态获取虚拟路径,帮助优化代码
  11. //tomcat7配置和tomcat8配置测试都能通过