web路径问题

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());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值