1. getRequestURI() :/test/TestURL/TestURL.jsp
2. getContextPath():/test
3. getServletPath():/TestURL/TestURL.jsp
三.web.xml中的路径
根路径: locolhost:8080/项目名/
Servlet 映射 <url-pattern>/xx</url-pattern> 中的 “/” 代表当前 web 应用的根路径。
这个只要思考一下就能得出结论:所有的web.xml文件都是描述某个web应用的部署相关信息,所以“/”只能代表当前 web 应用的根路径,而不是指向web站点的根目录
四.转发和重定向中的路径问题
1. 如果没有“/”,如request.getRequestDispatcher("hello.jsp ").forward(request, response) 和response.sendRedirect(" hello.jsp ") 就表示在同级目录中寻找login.jsp文件。
2. 如果路径中包含“/”(注意,这里的“/”是指路径中的第一个“/”)
a, 转发
如request.getRequestDispatcher("/hello.jsp").forward(request, response) :代表到http://localhost:8080/test/ 下目录寻找hello.jsp文件。
b,重定向
如response.sendRedirect("/hello.jsp"):代表到http://localhost:8080/ 目录下寻找hello.jsp文件
五.<form action=“/xxx”> 或 <a href=“/xxx”></a>中的路径 /request.getContext() + /servlet
“/”代表的是 web 站点的根路径.。
因为超链接可以链接到任何需要的目标资源, 所以 / 代表的肯定不是当前 web 应用的根路径, 而是当前 web 站点的根路径。
六.Page标签中errorPage属性的路径问题
errorPage属性值如果以”/”开头表示,表示相对于当前WEB应用程序的根目录,否则,表示相对与当前页面。
/*
* /test
* /test
* /test
* E:\apache-tomcat-7.0.78\wtpwebapps\test\
* /test
* E:\apache-tomcat-7.0.78\wtpwebapps\test\
* http://localhost:8080/test/CServlet
* /CServlet
*/
System.out.println(request.getContextPath());
System.out.println(request.getSession().getServletContext().getContextPath());
System.out.println(request.getServletContext().getContextPath());
System.out.println(request.getServletContext().getRealPath("/"));
System.out.println(request.getSession().getServletContext().getContextPath());
System.out.println(request.getSession().getServletContext().getRealPath("/"));
System.out.println(request.getRequestURL());
System.out.println(request.getServletPath());