---------------------------------相對路徑和絕對路徑訪問資源-------------------------------------------
相對路徑
在WebRoot下 新建 1.html
在WebRoot/aa 下新建 2.html
在1.html 和 2.html 分別通過 超鏈接 訪問 HelloServlet
路徑分爲相對路徑和絕對路徑兩種寫法
- 相對路徑,根據當前資源路徑 與 目標資源路徑,尋找相對位置關係,通過 . (當前目錄) 和 .. (上一級目錄) 訪問目標資源
1.html 訪問 HelloServlet
當前路徑 http://localhost/day5/1.html
目標路徑 http://localhost/day5/hello
位於同一個目錄中 ./hello 、hello ===== 替換當前路徑最後一個資源
2.html 訪問 HelloServlet
當前路徑 http://localhost/day5/aa/2.html
目標路徑 http://localhost/day5/hello
上一級目錄中 ../hello ===== 替換上一級目錄資源
***** 相對路徑,總需要分析當前路徑與目標路徑對應關係,編寫規則會根據當前路徑不同而不同
1.html
<h1>相對路徑</h1>
<a href="./hello">HelloServlet</a>
<a href="hello">HelloServlet</a>
2.html
<h1>相對路徑</h1>
<a href="../hello">HelloServlet</a>
代碼示例:
1html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>1.html相對路徑</h1> <!-- servlet路徑 http://localhost:8080/request_demo/hello 1.html路徑(當前目錄) http://localhost:8080/request_demo/dir1/1.html ../ 兩個點代表當前文件所屬文件夾的上一級路徑 --> <a href="../hello">1.html點擊訪問servlet</a> </body> </html> |
2html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>2.html相對路徑</h1> <!-- servlet路徑 http://localhost:8080/request_demo/hello 2.html路徑(當前目錄) http://localhost:8080/request_demo/2.html ./ 代表文件所屬的目錄 ./hello --> <a href="./hello">點擊訪問servlet</a> <!-- ./hello = hello 如果是統計目錄訪問可以省略./ --> <a href="hello">點擊訪問servlet</a> </body> </html> |
HelloServlet
package com.rl.servlet;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet被訪問了"); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
} |
Web.html
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.rl.servlet.HelloServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>HelloServlet</servlet-name> <!-- /相當於 = http://localhost:8080/request_demo/ --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> |
絕對路徑
1.html
<h1>絕對路徑</h1>
<a href="http://localhost/day5/hello">HelloServlet</a>
<a href="/day5/hello">HelloServlet</a>
2.html
<h1>絕對路徑</h1>
<a href="http://localhost/day5/hello">HelloServlet</a>
<a href="/day5/hello">HelloServlet</a>
帶有協議完整路徑 (跨網站) http://localhost/day5/hello
以/ 開始路徑 (同一個站點內) : /day5/hello
服務器端和客戶端對於/ 的區別
客戶端訪問路徑:/day5/hello
服務器內部路徑:/hello, 多用於服務器跳轉, 頁面包含(jsp)
代碼1.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>1.html相對路徑</h1> <!-- servlet路徑 http://localhost:8080/request_demo/hello 1.html路徑(當前目錄) http://localhost:8080/request_demo/dir1/1.html ../ 兩個點代表當前文件所屬文件夾的路徑的上一級路徑 --> <a href="../hello">1.html點擊訪問servlet</a>
<hr> <!-- 第一種絕對路徑訪問目標地址的方式 --> <h1>1.html絕對路徑</h1> <a href="http://localhost:8080/request_demo/hello">1.html絕對路徑訪問servlet</a> </body> </html> |
代碼2.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>2.html相對路徑</h1> <!-- servlet路徑 http://localhost:8080/request_demo/hello 2.html路徑(當前目錄) http://localhost:8080/request_demo/2.html ./ 代表文件所屬的目錄 ./hello --> <a href="./hello">點擊訪問servlet</a> <!-- ./hello = hello 如果是統計目錄訪問可以省略./ --> <a href="hello">點擊訪問servlet</a>
<hr> <!-- 第一種絕對路徑訪問目標地址的方式 缺點:寫死了IP和端口,這樣的不靈活,一旦ip和端口改變,所有的連接就都廢了,不建議使用這種方式 --> <h1>2.html絕對路徑</h1> <a href="http://localhost:8080/request_demo/hello">2.html絕對路徑訪問servlet第一種方式</a>
<!-- 使用/項目名稱/目標路徑, /代表絕對路徑項目名稱之前的路徑 --> <a href="/request_demo/hello">2.html絕對路徑訪問servlet第二種方式</a> </body> </html> |
Servlet代碼
package com.rl.servlet;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet被訪問了"); //從servlet中訪問1.html //全部路徑 http://localhost:8080/request_demo/dir1/1.html //在servlet中使用/代表http://localhost:8080/request_demo路徑 //所以/dir1/1.html等同於http://localhost:8080/request_demo/dir1/1.html request.getRequestDispatcher("/dir1/1.html").forward(request, response); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
} |