javaweb中路徑的書寫總結
阿新 • • 發佈:2019-01-01
在寫javaweb專案的時候,總會遇到路徑書寫的問題,現在將其作個總結。
在javaweb中需要書寫路徑的地方主要有這四大類:
客服端路徑
超連結
表單
重定向
伺服器端路徑
轉發
包含
資源獲取路徑
servletContext獲取資源
ClassLoader獲取資源
Class獲取資源
<url-pattern>路徑
現分別作介紹
其構建的javaweb如下:
1客服端路徑
A超連結
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>頁面A</title> </head> <body> <!-- 超連結和表當都有三種書寫路徑的方式 1,絕對地址 2,以"/"開頭的相對地址 3,不以"/"開頭的相對地址 --> <!-- 1.絕對地址 --> <!-- 完整的URL --> <a href="http://localhost:8080/javaee/jsp/b.jsp">這是絕對地址超連結</a><br/> <!-- 2.以"/"開頭的相對地址 --> <!-- /代表了整個web專案,即:http://localhost:8080/ --> <a href="/javaee/jsp/b.jsp">這是以"/"開頭的相對地址超連結</a><br/> <!-- 3.不以"/"開頭的相對地址 --> <!-- 不以/開頭,則相對於當前資源的路徑 當前資源的路徑為:http://localhost:8080/javaee/jsp/ 而b.jsp也在此路徑下 所以直接書寫b.jsp --> <a href="b.jsp">這是不以"/"開頭的相對地址超連結</a><br/> </body> </html>
B表單
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 所有的表單都是提交到b.jsp --> <!-- 表當提交路徑有三種書寫方式 1,絕對地址 2,以"/"開頭的相對地址 3,不以"/"開頭的相對地址 --> <form action="http://localhost:8080/javaee/jsp/b.jsp" methoe="get"> username:<input type="text" name="username" value=""> <input type="submit" value="提交---絕對地址 "> </form> <!-- 以/開頭的相對地址 此時的/代表整個web專案,即:http://localhost:8080/ --> <form action="/javaee/jsp/b.jsp" methoe="get"> username:<input type="text" name="username" value=""> <input type="submit" value="提交---以/開頭的相對地址"> </form> <form action="b.jsp" methoe="get"> username:<input type="text" name="username" value=""> <input type="submit" value="提交---不以/開頭的相對地址 "> </form> <!-- 表單提交到Servlet --> <!-- 表單提交到Servlet有三種書寫方式 1,絕對路徑 2,以"/"開頭的相對地址 3,不以"/"開頭的相對地址 --> <!-- 1.絕對地址 --> <!-- 完整的URL --> <form action="http://localhost:8080/javaee/PathServlet" methoe="get"> username:<input type="text" name="username" value=""> <input type="submit" value="表單提交到Servlet---絕對地址"> </form> <!-- 2.以/開頭的相對地址 --> <!-- 此時的/代表整個web專案,即:http://localhost:8080/ --> <form action="/javaee/PathServlet" methoe="get"> username:<input type="text" name="username" value=""> <input type="submit" value="表單提交到Servlet---以/開頭的相對地址"> </form> <!-- 3.不以/開頭的相對地址 --> <!-- 不以/開頭的相對路徑是相對於當前資源的路徑 此時form.jsp的地址為:http://localhost:8080/javaee/jsp/form.jsp 所以當前資源路徑為:http://localhost:8080/javaee/jsp 而要提交的Servlet的路徑為Http://localhost:8080/javaee/PathServlet 所以路徑為當前路徑的上一級路徑下 即路徑為:../PathServlet 注:.代表當前路徑 ..代表當前路徑的上一級路徑 --> <form action="../PathServlet" methoe="get"> username:<input type="text" name="username" value=""> <input type="submit" value="表單提交到Servlet---不以/開頭的相對地址"> </form> </body> </html>
C重定向
package cn.ccnu.path;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 重定向有三種路徑書寫方式
* 1.絕對路徑
* 2.以"/"開頭的相對路徑
* 3.不以"/"開頭的相對路徑
*/
public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("http://localhost:8080/javaee/jsp/b.jsp");
/*
* 2.以"/"開頭的相對路徑
* 此時,/代表整個web工程的路徑,即http://localhost:8080/
*/
// response.sendRedirect("/javaee/jsp/b.jsp");
/*
* 3.不以"/"開頭的相對路徑
* 此時是相對於當前資源的相對路徑
* 當前資源路徑為:http://localhost:8080/javaee/RedirectServlet
* 即表示:RedirectServlet在路徑http://localhost:8080/javaee之下
* 而b.jsp在http://localhost:8080/javaee/jsp/b.jsp
* 所以最終地址寫為:jsp/b.jsp
*/
// response.sendRedirect("jsp/b.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2伺服器端路徑
A請求轉發
package cn.ccnu.path;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 伺服器端的路徑不能是絕對路徑,只能是相對路徑,也分為以/開頭和不以/開頭兩種
* 1.以"/"開頭的相對路徑
* 2.不以"/"開頭的相對路徑
*/
public class DispatcherServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 1.以"/"開頭的相對路徑
* 此時,/代表當前web專案,即:http://localhost:8080/javaee
*/
// request.getRequestDispatcher("/jsp/b.jsp").forward(request, response);
/*
* 2.不以"/"開頭的相對路徑
* 相對於當前資源的相對路徑
* 此時,當前資源的路徑為:http://localhost:8080/javaee/DispatcherServlet
* 所以要轉發去的資源的路徑以:http://localhost:8080/javaee開頭
*/
request.getRequestDispatcher("jsp/b.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
B請求包含
package cn.ccnu.path;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 請求包含不能書寫絕對地址,只能書寫相對地址
* 1.以"/"開頭的相對路徑
* 2.不以"/"開頭的相對路徑
*
*/
public class IncludeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 1.以"/"開頭的相對路徑
* 此時,/代表當前web專案,即:http://localhost:8080/javaee
*/
// request.getRequestDispatcher("/jsp/b.jsp").include(request, response);
/*
* 2.不以"/"開頭的相對路徑
* 相對於當前資源的相對路徑
* 此時,當前資源的路徑為:http://localhost:8080/javaee/IncludeServlet
* 所以要轉發去的資源的路徑以:http://localhost:8080/javaee開頭
*/
request.getRequestDispatcher("jsp/b.jsp").include(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
3資源獲取路徑
AServletContext獲取資源
package cn.ccnu.path;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* ServletContext獲取資源必須是相對路徑,不能是絕對路徑,但不管是以/開頭,還是不以/開頭,
* 都是相對於當前資源的相對路徑
*
*/
public class ServletContextServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path1 = this.getServletContext().getRealPath("/a.properties");
String path2 = this.getServletContext().getRealPath("a.properties");
System.out.println(path1);
System.out.println(path2);
//輸出的地址一樣
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
BClassLoader獲取資源
package cn.ccnu.classloaderpath;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* ClassLoader類載入器不能通過絕對地址來載入資源,只能通過相對地址來載入資源
* 但相對地址不管前面加不加/都是相當於類路徑的相對地址
*
*/
public class ClassLoaderServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 加了/,其地址是相對於類路徑的相對地址
*/
// InputStream in = this.getClass().getClassLoader().getResourceAsStream("/cn/ccnu/classloaderpath/c.properties");
// Properties prop = new Properties();
// prop.load(in);
// System.out.println(prop.getProperty("url"));
/*
* 不加/,其地址是相對於類路徑的相對地址
*/
InputStream in = this.getClass().getClassLoader().getResourceAsStream("cn/ccnu/classloaderpath/c.properties");
Properties prop = new Properties();
prop.load(in);
System.out.println(prop.getProperty("url"));
/*
* 總結:不能使用絕對地址,而只能只用相對地址
* 且不管加不加/的相對地址,都是相對於類路徑的相對地址
*
*/
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
CClass獲取資源
package cn.ccnu.classpath;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* Class讀取資源不能是絕對路徑,只能是相對路徑,又分為以/開頭或者是不以/開頭
* 1.以/開頭的相對路徑
* 2.不以/開頭的相對路徑
*/
public class ClassServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 1.以/開頭的相對路徑
* 此時的/代表類路徑,即:/javaee/WEB-INF/classes
*/
// InputStream in = ClassServlet.class.getResourceAsStream("/cn/ccnu/classpath/b.properties");
// Properties porp = new Properties();
// porp.load(in);
// System.out.println(porp.getProperty("url"));
/*
* 2.不以/開頭的相對路徑
* 此時相對的是:類ClassServlet.class的路徑,即:\javaee\WEB-INF\classes\cn\ccnu\classpath
* 即:/javaee/WEB-INF/classes/cn/ccnu/classpath
*/
InputStream in = ClassServlet.class.getResourceAsStream("b.properties");
Properties porp = new Properties();
porp.load(in);
System.out.println(porp.getProperty("url"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4<url-pattern>路徑
要麼以“*”開關,要麼為“/”開頭,當通常情況看下,我們都會以"/"開頭。