路径

1、与路径相关的操作

  1. 超链接
  2. 表单
  3. 转发
  4. 包含
  5. 重定向
  6. <url-pattern>
  7. ServletContext获取资源
  8. Class获取资源
  9. ClassLoader获取资源

2、客户端路径

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

  1. 绝对路径;
  2. 以“/”开头的相对路径;
  3. 不以“/”开头的相对路径;
绝对路径:<a href="http://localhost:8080/hello2/index.html">链接1</a>
客户端路径:<a href="/hello3/pages/index.html">链接2</a>
相对路径:<a href="index.html">链接3</a>
<hr/>
绝对路径:
<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>

重定向1

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        response.sendRedirect("/test1/html/test1.html");
    }

​ 假设访问IServlet的路径为:http://localhost:8080/test1/IServlet

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

重定向2

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        response.sendRedirect("test3.html");
    }

假设访问IServlet的路径为:http://localhost:8080/test1/AServlet

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

建议使用“/”

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

<form action="/hello/servlet/AServlet">
</form>
<a href="/hello/b.html">链接</a>

3、服务器端路径

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

  1. 以“/”开头;
  2. 不以“/”开头;

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

  1. 客户端路径以“/”开头:相对当前主机;
  2. 服务器端路径以“/”开头:相对当前应用;

4、<url-pattern>路径

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

  <servlet-mapping>
    <servlet-name>SendEmail</servlet-name>
    <url-pattern>/SendEmail</url-pattern>
  </servlet-mapping>

5、ServletContext获取资源

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

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        //response.sendRedirect("test3.html");
        String path1 ;
        String path2 ;
        path1 = this.getServletContext().getRealPath("/test3.html");
        path2 = this.getServletContext().getRealPath("test3.html");
        System.out.println(path1);
        System.out.println(path2);
    }
E:\tomcat\apache-tomcat-8.5.46\wtpwebapps\test1\test3.html
E:\tomcat\apache-tomcat-8.5.46\wtpwebapps\test1\test3.html

6、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类在cn.itcast包下,所以资源路径为:/hello/WEB-INF/classes/cn/itcast/a.txt。

7、classLoder获取资源

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

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

Last modification:November 23rd, 2019 at 05:27 am
如果觉得我的文章对你有用,请随意赞赏