1. 程式人生 > >WEB專案的相對路徑與絕對路徑

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