WEB项目的相对路径与绝对路径

1 与路径相关的操作

l  超链接

l  表单

l  转发

l  包含

l  重定向

l  <url-pattern>

l  ServletContext获取资源

l  Class获取资源

l  ClassLoader获取资源

 

先说结论:

1、强烈建议使用“/”开头的路径

2、超链接、表单、重定向:以“/”开头的的路径相对于主机根目录http://localhost:8080/

  转发、包含、<url-pattern>:以“/”开头的的路径相对项目根目录http://localhost:8080/项目名称/

3、注意:不带“/”的相对路径,是相对于访问到当前文件的路径,而不是当前文件所在的目录

 】


2 客户端路径

超链接、表单、重定向都是客户端路径,客户端路径可以分为三种方式:

l  绝对路径;

l  以“/”开头的相对路径;

l  不以“/”开头的相对路径;

例如:http://localhost:8080/hello1/pages/a.html中的超链接和表单如下:

绝对路径:<ahref="http://localhost:8080/hello2/index.html">链接1</a>

客户端路径:<ahref="/hello3/pages/index.html">链接2</a>

相对路径:<ahref="index.html">链接3</a>

<hr/>

绝对路径:

<formaction="http://localhost:8080/hello2/index.html">

  <input type="submit"value="表单1"/>

</form>

客户端路径:

<formaction="/hello2/index.html">

  <input type="submit"value="表单2"/>

</form>

相对路径:

<formaction="index.html">

  <input type="submit"value="表单3"/>

</form>

 

l  链接1和表单1:没什么可说的,它使用绝对路径;

l  链接2和表单2:以“/”开头,相对主机,与当前a.html的主机相同,即最终访问的页面为http://localhost:8080/hello2/index.html;

l  链接3和表单3:不以“/”开头,相对当前页面的路径,即a.html所有路径,即最终访问的路径为:http://localhost:8080/hello1/pages/index.html;

 

重定向1:

publicclass AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

          throws ServletException, IOException {

       response.sendRedirect("/hello/index.html");

    }

}

 

  假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

  因为路径以“/”开头,所以相对当前主机,即http://localhost:8080/hello/index.html。

 

重定向2:

publicclass AServlet extends HttpServlet {

    publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       response.sendRedirect("index.html");

    }

}

 

假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

因为路径不以“/”开头,所以相对当前路径,即http://localhost:8080/hello/servlet/index.html

 

2.1 建议使用“/”

强烈建议使用“/”开头的路径,这说明在页面中的超链接和表单都要以“/”开头,后面是当前应用的名称,再是访问路径:

<formaction="/hello/servlet/AServlet">

</form>

<a href="/hello/b.html">链接</a>

 

其中/hello是当前应用名称,这也说明如果将来修改了应用名称,那么页面中的所有路径也要修改,这一点确实是个问题。这一问题的处理方案会在学习了JSP之后讲解!

 

在Servlet中的重定向也建议使用“/”开头。同理,也要给出应用的名称!例如:

response.sendRedirect("/hello/BServlet");

 

其中/hello是当前应用名,如果将来修改了应用名称,那么也要修改所有重定向的路径,这一问题的处理方案是使用request.getContextPath()来获取应用名称。

response.sendRedirect(request.getContextPath() +"/BServlet");

 

3 服务器端路径

服务器端路径必须是相对路径,不能是绝对路径。但相对路径有两种形式:

  以“/”开头;

  不以“/”开头;

 

其中请求转发、请求包含都是服务器端路径,服务器端路径与客户端路径的区别是:

  客户端路径以“/”开头:相对当前主机;

  服务器端路径以“/”开头:相对当前应用;

 

转发1:

publicclass AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

          throws ServletException, IOException {

       request.getRequestDispatcher("/BServlet").forward(request, response);

    }

}

 

假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

因为路径以“/”开头,所以相对当前应用,即http://localhost:8080/hello/BServlet。

 

转发2:

publicclass AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

          throws ServletException, IOException {

       request.getRequestDispatcher("BServlet").forward(request, response);

    }

}

 

假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

因为路径不以“/”开头,所以相对当前应用,即http://localhost:8080/hello/servlet/BServlet。

 

4 <url-pattern>路径

  <url-pattern>必须使用“/”开头,并且相对的是当前应用。

 

5 ServletContext获取资源

必须是相对路径,可以“/”开头,也可以不使用“/”开头,但无论是否使用“/”开头都是相对当前应用路径。

例如在AServlet中获取资源,AServlet的路径路径为:http://localhost:8080/hello/servlet/AServlet:

publicclass AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

          throws ServletException, IOException {

       String path1 =this.getServletContext().getRealPath("a.txt");

       String path2 =this.getServletContext().getRealPath("/a.txt");

       System.out.println(path1);

       System.out.println(path2);

    }

}

 

path1和path2是相同的结果:http://localhost:8080/hello/a.txt

 

6 Class获取资源

Class获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。

package cn.itcast;

 

import java.io.InputStream;

 

publicclass Demo {

    public void fun1() {

       InputStream in = Demo.class.getResourceAsStream("/a.txt");

    }

   

    public void fun2() {

       InputStream in = Demo.class.getResourceAsStream("a.txt");

    }

}

 

其中fun1()方法获取资源时以“/”开头,那么相对的是当前类路径,即相对于classes目录,即/hello/WEB-INF/classes/a.txt文件;

其中fun2()方法获取资源时没有以“/”开头,那么相对当前Demo.class所在路径,因为Demo类在cn.itcast包下,所以资源路径为:/hello/WEB-INF/classes/cn/itcast/a.txt。

 

7 ClassLoader获取资源

ClassLoader获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。但无论是否以“/”开头,资源都是相对当前类路径。

publicclass Demo {

    public void fun1() {

       InputStream in = Demo.class.getClassLoader().getResourceAsStream("/a.txt");

    }

   

    public void fun2() {

       InputStream in = Demo.class.getClassLoader().getResourceAsStream("a.txt");

    }

}

 

  fun1()和fun2()方法的资源都是相对类路径,即classes目录,即/hello/WEB-INF/classes/a.txt

  • 15
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
前端开发中,相对路径绝对路径是用来指定文件或目录在文件系统中位置的两种方式。 相对路径是相对于当前文件所在的位置来指定文件或目录的路径。常见的相对路径有以下几种表达方式: - "./":表示当前目录,可以用来引用当前目录下的文件或子目录。 - "../":表示上一级目录,可以用来引用上一级目录下的文件或子目录。 - "../../":表示上上级目录,以此类推。 绝对路径则是从根目录开始指定文件或目录的完整路径。在Web开发中,绝对路径通常从根目录开始,以斜线"/"作为起始符,例如"/images/logo.png"表示根目录下的images目录中的logo.png文件。 相对路径绝对路径各有优缺点。相对路径的优点是可以相对灵活地指定文件或目录的位置,可以在不同环境中进行移动或测试。而绝对路径的优点是能够确保链接的准确性和稳定性,避免被恶意抄袭和避免链接指向错误的URL。 在前端开发中,选择使用相对路径还是绝对路径取决于具体情况和需求。一般来说,相对路径适用于同一网站内的文件引用,而绝对路径更适合确保链接的稳定性和准确性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [前端基础(三)_路径(绝对路径相对路径)、语义化、特殊字符](https://blog.csdn.net/qq_43291759/article/details/128304728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值