假设在myapp项目下有个login.html,index.jsp,还写了两个servletA和servletB.
在web.xml中的地址配置:
/servlet/servletA
/servlet/servletB
在情况一中:若在路径中以/开头,则这一/相当于http://localhost:8080/
1、login.html有个form表单有提交给servletA,那么action要填的路径:
绝对路径方式:action="/myapp/servlet/servletA" ------http://localhost:8080/myapp/servlet/servletA
相对路径方式:action="servlet/servletA" ------http://localhost:8080/myapp/servlet/servletA
2、login.html有个链接到index.jsp 那么
绝对路径方式:href="/myapp/index.jsp" ------http://localhost:8080/myapp/index.jsp
相对路径方式:action="index.jsp" ------http://localhost:8080/myapp/index.jsp
3、index.jsp中重定向到servletA
绝对路径方式:sendRedirect("/myapp/servlet/servletA"); ------http://localhost:8080/myapp/servlet/servletA
相对路径方式:sendRedirect("servlet/servletA"); ---http://localhost:8080/myapp/servlet/servletA
在情况二中:若在路径中以/开头,则这一/相当于http://localhost:8080/myapp/
1.servletA转发到servletB
绝对路径方式:request.getRequestDispatcher("/servlet/servletB").forward(request, response);
--------http://localhost:8080/myapp/servlet/servletB
相对路径方式:request.getRequestDispatcher("servlet/servletB").forward(request, response);
--------http://localhost:8080/myapp/servlet/servletB