1. 程式人生 > 其它 >Response_路徑_相對路徑以及絕對路徑

Response_路徑_相對路徑以及絕對路徑

Response_路徑_相對路徑以及絕對路徑

1.相對路徑:通過相對路徑不可以確定唯一資源

  如:./index.html

  不以/開頭,以.開頭路徑

規則:找到當前資源和目標資源之間的相對位置關係

  ./當前路徑

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>找到當前資源和目標資源的相對位置關係</h1
> <p> 當前資源:location.html http://localhost/location.html </p> <p> 目標資源: http://localhost/responseDemo2 </p> <a href="./responseDemo2"> responseDemo02 </a> </body> </html>

  ../:回退以及目錄

<!DOCTYPE html
> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>找到當前資源和目標資源的相對位置關係</h1> <p> 當前資源:location2.html http://localhost/htmls/location2.html </p> <p> 目標資源: http://localhost/responseDemo2
</p> <a href="../responseDemo2"> responseDemo02 </a> </body> </html>

2絕對路徑:通過絕對路徑可以確定唯一資源

  如:http://localhost/responseDemo2    

  以/開頭路徑

html頁面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <h1>絕對路徑</h1>
    <a href="/requestDemo2">
        requestDemo2
    </a>
</body>
</html>

內部使用:

@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //轉發
        request.getRequestDispatcher("/responseDemo2").forward(request,response);
    }

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

規則:判斷定義的路徑是給誰用的?判斷請求將來從哪發出

  給客戶端瀏覽器使用:需要加虛擬目錄(專案的訪問路徑)

    建議虛擬目錄動態獲取:request.getContextPath()

  給伺服器使用:不需要加虛擬目錄