ITeye博客

web应用 相对路径 绝对路径 总结

rayn115 · 2011-05-20 11:50 · Java · 编程语言 · Markdown

参考资料: 
http://www.iteye.com/topic/553749 
http://www.iteye.com/topic/718443 

上面两篇文章写的很好,我做下补充和总结。 

路径分绝对和相对两种 
  ./ 表示当前路径,../表示上一路径 
  直接写文件名. 或 ./  或 ../ 都是相对路径 除了这些写法都是绝对路径  a.jsp路径和代码如下 
+webRoot 
  -s1 
     a.jsp 

Java代码  收藏代码

  1. //我的服务器地址加上应用地址是 “http://localhost:8080/imageShow/”,文件夹路径为
  2. //
  3. // 实际路径 http://localhost:8080/imageShow/s1/s1/b.html
  4. <a href="s1/b.html">b.html</a>
  5. // 实际路径 http://localhost:8080/imageShow/b.html
  6. <a href="../b.html">b.html</a>
  7. //实际路径 http://localhost:8080/imageShow/s1/b.html
  8. <a href="./b.html">b.html</a>
  9. // 实际路径 http://localhost:8080/imageShow/s1/b.html  与上一个样
  10. <a href="b.html">b.html</a>

常见绝对路径 
1. http://localhost:8080/imageShow/includetest/s1/a.jsp 
2.F:\冰河\学习资料\开源 代码 struts2 pager 
3.以“ /”开头。 
1和2没什么好解释的 
“/”在jsp中表示 "http://机器IP:端口号/应用名/" 
注意jsp路径是指 jsp中除html标签以外的标签如 <%@include file"/b.html"%>或 <jsp:include page="/b.html"/> 

Java代码  收藏代码

  1. <%@include file"/s1/a.html"%>//这样才能找到a.html 这个是绝对路径写法
  2. jsp:include指令同理

“/”在Servlet中路径表示 

转发请求时:"/"表示“http://服务器IP:8080/Web应用名/”,例如:String forward = "/s1/a.jsp";    RequestDispatcher rd = request.getQRequestDispatcher(forward); 
重定向时:“/”  表示“http://机器IP:8080/”,而通过request.getContextPath()得到的是:“http://机器IP:8080/Web应用名/”,例如:String str =  request.getContextPath();response.sendRedirect(str + "/s1/a.jsp"); 
“/”在配置文件web.xml中 
   url-mapping中,"/"表示“http://IP地址:8080/Web应用名/” 

Java代码  收藏代码

  1. <welcome-file-list>
  2. <welcome-file>/index.jsp</welcome-file>
  3. </welcome-file-list>
  4. 在web.xml中 如index.jsp前不加"/" 会被默认加上

"/"在html,javacsript,css中均表示"http://IP地址:8080" 

根据http://www.iteye.com/topic/718443中的总结 

××总结×× 

    在浏览器端:“/”表示的是一台WEB服务器,“http://机器IP:8080/” 
    在服务器端(请求转发):“/”表示的是一个WEB服务器端的应用,“http://机器IP:8080/Web应用/” 
    在服务器端(重定向):“/”表示的是一个WEB服务器,“http://机器IP:8080/” 

sevlet在服务器端,而jsp是由servlet编译的,html,javacsript,css均在浏览器端。 
其中jsp是怎样确定路径的,暂时不清楚 

(转自: http://chenchangqun11.iteye.com/blog/831530)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。