JavaEE中關於路徑問題的總結

路徑問題總結

一丶與路徑相關的操作

    ①超鏈接
    ②表單
    ③包含
    ④重定向
    ⑤<url-pattern>
    ⑥ServletContext獲取資源
    ⑦Class獲取資源
    ⑧ClassLoader獲取資源

二丶客戶端路徑

    超鏈接、表單、重定向都是客戶端路徑,客戶端路徑可以分爲三種方式:
        ①絕對路徑;
        ②以“/”開頭的相對路徑;
        ③不以“/”開頭的相對路徑;
        例如:http://localhost:8080/Web_PathProblem/index.html中的超鏈接和表單如下:
絕對路徑:<a href="http://localhost:8080/Web_PathProblem/index.html">鏈接1</a><br/>
以“/”開頭的相對路徑:<a href="/Web_PathProblem/index.html">鏈接2</a><br/>
相對路徑:<a href="index.html">鏈接3</a><br/>
<hr/>
絕對路徑:
<form action="http://localhost:8080/Web_PathProblem/index.html">
<input type="submit"value="表單1"/>
<hr/>
</form>
相對路徑1:
<form action="/Web_PathProblem/index.html">
<input type="submit"value="表單2"/>
</form>
<hr/>
相對路徑2:
<form action="index.html">
<input type="submit"value="表單3"/>
</form>

上述代碼對應的顯示

點擊其中任何的連接都會跳轉到指定的界面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>welcome</h2>
</body>
</html>

顯示情況

重定向帶"/"的相對路徑:       
public class AServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("/Web_PathProblem/index.html");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

重定向不帶”/”的相對路徑:

public class AServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("index.html");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

三丶服務器端路徑

    服務器端路徑必須是相對路徑,不能是絕對路徑。但相對路徑有兩種形式:
        ①以“/”開頭;
        ②不以“/”開頭;
        其中請求轉發、請求包含都是服務器端路徑,服務器端路徑與客戶端路徑的區別是:
        客戶端路徑以“/”開頭:相對當前主機;
        服務器端路徑以“/”開頭:相對當前應用(項目名稱);

轉發1代碼:

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

假設訪問AServlet的路徑爲:http://localhost:8080/Web_PathProblem/servlet/AServlet
因爲路徑以“/”開頭,所以相對當前應用,即http://localhost:8080/Web_PathProblem/BServlet

轉發2代碼:

publicclass AServlet extends HttpServlet {
    publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("BServlet").forward(request, response);
    }
}

假設訪問AServlet的路徑爲:http://localhost:8080/Web_PathProblem/servlet/AServlet
因爲路徑不以“/”開頭,所以相對當前應用,即http://localhost:8080/Web_PathProblem/servlet/BServlet


四丶路徑

    <url-pattern>必須使用“/”開頭,並且相對的是當前應用。

五丶ServletContext獲取資源

    必須是相對路徑,可以“/”開頭,也可以不使用“/”開頭,但無論是否使用“/”開頭都是相對當前應用路徑。

例如在AServlet中獲取資源,AServlet的路徑路徑爲:http://localhost:8080/Web_PathProblem/servlet/AServlet

publicclass AServlet extends HttpServlet {
    publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path1 = this.getServletContext().getRealPath("Demo.txt");
        String path2 = this.getServletContext().getRealPath("/Demo.txt");
        System.out.println(path1);
        System.out.println(path2);
    }
}

路徑都相同


六丶Class獲取資源

    Class獲取資源必須是相對路徑,可以“/”開頭,也可以不使用“/”開頭。
        import java.io.InputStream;
public class Demo {
    publicvoid fun1() {
        InputStream in = Demo.class.getResourceAsStream("/a.txt");
    }

    publicvoid fun2() {
        InputStream in = Demo.class.getResourceAsStream("a.txt");
    }
}

其中fun1()方法獲取資源時以“/”開頭,那麼相對的是當前類路徑,即/Web_PathProblem/WEB-INF/classes/a.txt文件;
其中fun2()方法獲取資源時沒有以“/”開頭,那麼相對當前Demo.class所在路徑,因爲Demo類在cn.itcast包下,所以資源路徑爲:/Web_PathProblem/WEB-INF/classes/com/demo/servlet/a.txt。


七丶ClassLoader獲取資源

    ClassLoader獲取資源也必須是相對路徑,可以“/”開頭,也可以不使用“/”開頭。但無論是否以“/”開頭,資源都是相對當前類路徑。
publicclass Demo {
    publicvoid fun1() {
        InputStream in = Demo.class.getClassLoader().getResourceAsStream("/a.txt");
    }

    publicvoid fun2() {
        InputStream in = Demo.class.getClassLoader().getResourceAsStream("a.txt");
    }
}

fun1()和fun2()方法的資源都是相對類路徑,即classes目錄,即/Web_PathProblem/WEB-INF/classes/a.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章