---
title: "web应用 相对路径 绝对路径 总结"
author: "rayn115"
date: "2011-05-20T11:50:02Z"
tags: []
categories: ["编程语言"]
slug: "web"
draft: false
lastmod: "2011-05-20T11:50:02Z"
id: 1051444
url: "https://rayn115.iteye.com/blog/1051444"
---

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

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

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

Java代码  [![收藏代码](http://chenchangqun11.iteye.com/images/icon_star.png)](http://chenchangqun11.iteye.com/blog/831530 "收藏这段代码")

01. //我的服务器地址加上应用地址是 “http://localhost:8080/imageShow/”，文件夹路径为
02. //
03. // 实际路径 http://localhost:8080/imageShow/s1/s1/b.html
04. &lt;a href="s1/b.html"&gt;b.html&lt;/a&gt;
05. // 实际路径 http://localhost:8080/imageShow/b.html
06. &lt;a href="../b.html"&gt;b.html&lt;/a&gt;
07. //实际路径 http://localhost:8080/imageShow/s1/b.html
08. &lt;a href="./b.html"&gt;b.html&lt;/a&gt;
09. // 实际路径 http://localhost:8080/imageShow/s1/b.html  与上一个样
10. &lt;a href="b.html"&gt;b.html&lt;/a&gt;

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

Java代码  [![收藏代码](http://chenchangqun11.iteye.com/images/icon_star.png)](http://chenchangqun11.iteye.com/blog/831530 "收藏这段代码")

1. &lt;%@include file"/s1/a.html"%&gt;//这样才能找到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代码  [![收藏代码](http://chenchangqun11.iteye.com/images/icon_star.png)](http://chenchangqun11.iteye.com/blog/831530 "收藏这段代码")

1. &lt;welcome-file-list&gt;
2. &lt;welcome-file&gt;/index.jsp&lt;/welcome-file&gt;
3. &lt;/welcome-file-list&gt;
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](http://chenchangqun11.iteye.com/blog/831530))