以下四种情况,总结我们在Servlet配置路径时的步骤。
首先你先写个 /
然后看是哪种行为 ,服务器行为/是相对webroot , 浏览器行为/是相对webapps。
相对路径:当前路径相对于java命令运行的bin目录。
在web项目中,java命令运行的目录就是在tomcat的bin目录。
转发: 服务器行为
request.getRequestDispatcher("/hello.html").forward(request, response);
重定向: 浏览器行为
response.sendRedirect("/ServletContext/hello.html");
所以重定向可以重定向一个站点的所有项目, 转发只能是当前项目类的资源
超链接 :浏览器行为
response.getWriter().write("<a href=/ServletContext/hello.html>link</a>");
表单提交 : 浏览器行为
response.getWriter().write("<form method='get' action='/ServletContext/hello.html'><input type='submit'/></form>");
总结:看服务器行为还是浏览器行为。