• 路径


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

    绝对路径:  <a href="http://localhost:8080/hello2/index.html">链接1</a>
    客户端路径:<a href="/hello2/pages/index.html">链接2</a>项目名+包名+类名
    相对路径:  <a href="index.html">链接3</a>当前类下的index.html

    重定向路径加/:项目名加类名,

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

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

    不加/就是直接

    response.sendRedirect("BServlet");

    二:服务器端路径必须是相对路径,不能是绝对路径。请求转发、请求包含,<url-pattern>路径都是服务器端路径  http://localhost:8080/项目名

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

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

    三:ServletContext获取资源

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

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

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

    四:Class获取资源

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

    Demo.class.getResourceAsStream("/a.txt");

    Demo.class.getResourceAsStream("a.txt");

    获取资源时以“/”开头,那么相对的是当前类路径,/hello/WEB-INF/classes/a.txt文件;默认在编译后的classes包下面

    获取资源时没有以“/”开头,那么相对当前Demo.class所在路径,因为Demo类在com.sxt包下,所以资源路径为:/hello/WEB-INF/classes/com/sxt/a.txt。默认和当前类在同一个包下面

    五:ClassLoader获取资源

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

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

    都是相对类路径,即classes目录,即/hello/WEB-INF/classes/a.txt;默认在编译后的classes包下面

  • 相关阅读:
    HTTP && 缓存
    querySelector $() getElementBy区别
    Canvas 雾玻璃
    Github page
    Browserify
    关于布局和结构
    how to install flash
    kali update can’t found win7 loader
    arp spoofing
    How To Set Up Port Forwarding in Linux
  • 原文地址:https://www.cnblogs.com/lndbky/p/13372422.html
Copyright © 2020-2023  润新知