javaweb教程 javaweb項目路徑總結

javaweb源代碼目錄與服務器中javaweb目錄對比

  • javaweb的源代碼目錄
|--- pom.xml
|--- src
      |--- main
			|--- java
			|--- source
	  |--- test
|--- web
	  |--- WEB-INF
			 |--- web.xml
  • javaweb編譯打包之後發佈到服務器中的目錄
|--- WEB-INF
		|--- jsp
			  |--- index.jsp			  	
 		|--- classes
 		      |--- java源文件編譯存放的目錄	
		|--- web.xml 
|--- index.html
  • 關於javaweb的源代碼目錄 與 打包發佈的目錄轉換
    在這裏插入圖片描述
    • 源碼工程中java目錄中所有.java源碼被編譯到了 發佈包中的classes目錄。(java目錄中的資源文件不會被拷貝到classes目錄中,但是如果必須把資源文件放到java目錄中,可以使用maven插件拷貝java目錄中的資源文件。)
    • 源碼工程中的Web目錄,被編譯到了項目的根目錄。源碼Web目錄中資源文件被編譯到項目的根目錄,然後源碼Web目錄中中的WEB-INF內的文件,被拷貝到了打包後的WEB-INF中。



發佈後的項目根路徑獲取

  • 獲取項目的根路徑 (指的是web的上下文映射路徑)
    這個不是真實的磁盤路徑,而是用於url上的虛擬映射路徑。
#項目的根路徑. 這個不是真實的磁盤路徑,而是用於url上的虛擬映射路徑。
http://localhost:8080/test/hello 就是指的/test
String path = req.getServletContext().getContextPath();
System.out.println(path);
  • 獲取當前的servlet的映射路徑
http://localhost:8080/test/hello 就是指的/hello
String servletPath = req.getServletPath();
System.out.println(path)
  • 獲取當前項目的磁盤路徑(用於讀取文件)
#然後憑藉項目根路徑的磁盤路徑
String filePath = req.getServletContext().getRealPath(”“);
System.out.println(filePath);



獲取URL請求的靜態資源的真實路徑

  • 獲取靜態資源的URI,就是相對於項目的。而servletContext.getRealPath("")可以獲取到項目根路徑的磁盤路徑。所以可以將URI對應的資源,轉發成磁盤上該資源的路徑。
  • getContextPath() 與 getRealPath()沒有半毛錢關係。且互相不會影響。
#比如 http://www.baidu.com/test/index.html
#1. 先獲取URL上的URI,即/test/index.html。
#2. 然後去除掉getContextPath()的虛擬映射路徑,則剩下index.html.表示位於項目的根目錄下。
#3. 然後servletContext.getRealPath("/index.html"),就能獲取到該文件的真實路徑了。

#獲取路徑
String filePath = req.getServletContext().getRealPath("/index.html");

#讀取路徑下的資源
InputStream inputStream =  req.getServletContext().getResourceAsStream("/index.html");



項目的類路徑獲取與加載類路徑下資源

項目的類路徑,就是指 “項目/WEB-INF/classes” 的路徑

  • 通過普通類的classLoader獲取項目的類路徑。
#獲取classes的磁盤路徑。
URL url = HelloServlet.class.getClassLoader().getResource("");
String classesPath = url.getFile();

#獲取classes下的磁盤路徑,然後拼裝路徑,獲取classes下的資源文件。
Properties properties = new Properties();
properties.load(new FileInputStream(new File(url.getFile()+"/a.properties")));
System.out.println(properties.get("name"));

#當然,你也可以直接使用如下方法。(相對於classess路徑)
InputStream inputStream =  HelloServlet.class.getClassLoader().getResourceAsStream("/a.properties");
properties.load(inputStream);
System.out.println(properties.get("name"));
  • 通過獲取項目的磁盤路徑,並且拼裝/WEB-INF/classes
#獲取項目的磁盤路徑。
ServletContext servletContext = req.getServletContext();
#根據項目的磁盤路徑,拼裝出classes下的路勁。
String classesPath = servletContext.getRealPath("")+"/WEB-INF/classes";
System.out.println(classesPath);

#獲取classes目錄下的資源。
Properties properties = new Properties();
properties.load(new FileInputStream(new File(classesPath+"/a.properties")));
System.out.println(properties.get("name"));



重定向與請求轉發的路徑使用

  • 相對路徑

    • 服務器中設置重定向,或者請求轉發的相對路徑,是相對於當前class文件所在的路徑。不推薦使用,容易出現錯誤。

  • 絕對路徑: (推薦使用)
    *請求轉發的絕對路徑是 相對於 “域名/服務器的根路徑” 的位置

    #跳轉的路徑就是 http://localhost:8080/test/index.html
    #請求轉發使用絕對路徑,所以是相對於http://localhost:8080/test/路徑。
    
    RequestDispatcher requestDispatcher = req.getRequestDispatcher("/index.html");
    requestDispatcher.forward(req, resp);
    
    • 重定向的絕對路徑是相對於域名的。(不包含服務器的根路徑)
    #跳轉的路徑就是 http://localhost:8080/test/index.html
    #重定向使用絕對路徑,所以是相對於http://localhost:8080/路徑。
    
    #需要自己加上一個項目根路徑。
    resp.sendRedirect( req.getServletContext().getContextPath()+"/index.html");
    

  • url路徑

#重定向支持 http,https協議開頭的url跳轉
resp.sendRedirect( "http://www.baidu.com");



瀏覽器中html中的路徑使用

  • 絕對路徑:
    瀏覽器中的絕對路徑,是相對於域名的。
#<a>標籤 設置絕對路徑之後,訪問的URL爲: http://localhost:8080/hello2
<a href="/hello2">這是一個跳轉連接</a>	

#<form>表單
<form method="POST" action="/hello3">
	<input type="submit" value="提交">
</form>
  • 相對路徑
    瀏覽器中的相對路徑,默認是相對於當前頁面。容易出錯誤,不推薦使用。

  • <base>的相對路徑
    瀏覽器在head中可以指定一個<base>標籤,指定相對路徑的基準路徑。用於解決絕對路徑不能帶有應用上下文的問題

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <base href="http://localhost:8080/test/">
</head>
<body>
    <a href="./hello2">這是一個錨點</a>
    <form action="/hello3" method="post">
        <input type="submit" value="點擊提交">
    </form>
</body>
</html>



獲取classes下的properties配置文件

#a.properties文件必須放在 classes 目錄下。
InputStream inputStream =  HelloServlet.class.getClassLoader().getResourceAsStream("/a.properties");
properties.load(inputStream);
System.out.println(properties.get("name"));
發佈了49 篇原創文章 · 獲贊 34 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章