1.与路径相关的操作

1.超链接

不以“/”开头的相对路径;相对当前的文件夹路径 如果在根目录下面也是可以的

例如:http://localhost:8080/hello2/pages/a.html中的超链接和表单如下:
绝对路径:  <a href="http://localhost:8080/hello2/index.html">链接1</a>
客户端路径:<a href="/hello2/pages/index.html">链接2</a>
相对路径:  <a href="index.html">链接3</a>

链接1和表单1:它使用绝对路径;
链接2和表单2:以“/”开头,相对主机,与当前a.html的主机相同,即最终访问的页面为http://localhost:8080/hello2/index.html;
链接3和表单3:不以“/”开头,相对当前页面的路径,即a.html所有路径,即最终访问的路径为:http://localhost:8080/hello2/pages/index.html;
2.表单

以“/”开头的相对路径 / 相对的是 localhost:8080 路径
正确写法: /项目名称/xxx/xxx
不以“/”开头的相对路径;相对当前的文件夹路径

</form>
绝对路径:
<form action="http://localhost:8080/hello2/index.html">
  <input type="submit" value="表单1"/>
</form>
客户端路径:
<form action="/hello2/index.html">
  <input type="submit" value="表单2"/>
</form>
相对路径:
<form action="index.html">
  <input type="submit" value="表单3"/>
</form>
3.转发

如果该路径以 “/” 开头,那么可以相对于当前上下文根解释它
hello(不加/ 也可以 )
req.getRequestDispatcher("/AServlet/demo02.html").forward(req, resp);

public class AServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/BServlet").forward(request, response);
    }
}

假设访问AServlet的路径为:http://localhost:8080/hello/AServlet
因为路径以“/”开头,所以相对当前应用,即http://localhost:8080/hello/BServlet。
4.重定向

如果位置是相对的,有一个前导 ‘/’,则容器将相对于 servlet 容器根对其进行解释。
容器根 = localhost:8080

重定向1:
public class AServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect("/hello/index.html");
    }
}

重定向2:
public class AServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //相对/hello 这个路径下面
        response.sendRedirect("index.html");
    }
}

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


链接 其中/hello是当前应用名称,这也说明如果将来修改了应用名称,那么页面中的所有路径也要修改,这一点确实是个问题。这一问题的处理方案会在学习了JSP之后讲解! 在Servlet中的重定向也建议使用“/”开头。同理,也要给出应用的名称!例如:

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

response.sendRedirect(request.getContextPath() + “/BServlet”);

5.<–url-pattern–>

<–url-pattern>必须使用“/”开头,并且相对的是当前应用。
路劲匹配 /demo01 要加 / 相对 /hello/下面的路劲
扩展名匹配 .do 或者.action 一扩展结尾都可以访问
通配符

6.ServletContext获取资源

context.getRealPath("/");
真实路径: / 当前项目 webContent目录下面
找WEB-INF/lib 下面路径: getRealPath("/WEB-INF/xxx");
src下面的路劲: web项目 src下面java文件都编译了 放在
/WEB-INF/classes 下面
getRealPath("/WEB-INF/classes/xxx")

必须是相对路径,可以“/”开头,也可以不使用“/”开头,但无论是否使用“/”开头都是相对当前应用路径。
例如在AServlet中获取资源,AServlet的路径路径为:http://localhost:8080/hello/servlet/AServlet:

public class 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

7.Class获取资源

class 获取资源是classes 下面的资源
加 / 获取的是classes 下面的路劲
不加/ 获取的是classes 下面包中的具体内的当前路劲
/classes/com/xxx/xxx
Class获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。

import java.io.InputStream;

public class Demo {
    public void fun1() {
        InputStream in = Demo.class.getResourceAsStream("/a.txt");
    }
    
    public void fun2() {
        InputStream in = Demo.class.getResourceAsStream("a.txt");
    }
}

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

8.ClassLoader获取资源

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

public class 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