System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getFile());
// System.out.println(ClassLoader.getSystemResource("").getFile());
System.out.println(this.getClass().getClassLoader().getResource("").getFile());
System.out.println(this.getClass().getResource("/").getFile());
System.out.println(new File("bin").getCanonicalPath());
System.out.println(request.getServletContext().getRealPath(""));
System.out.println( request.getSession().getServletContext().getRealPath("/"));
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
public PropertiesParameters load(InputStream inputStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
while(true) {
String line = reader.readLine();
if (line == null) {
return this;
}
line = line.trim();
if (line.length() != 0 && !line.startsWith("#")) {
StringTokenizer tokenizer = new StringTokenizer(line, "= :", false);
String key = tokenizer.nextToken();
String value = tokenizer.nextToken();
this.parameters.put(key, value);
}
}
} catch (IOException var7) {
throw new RuntimeException(var7);
}
}
<%@ 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>
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);
}
}
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;
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);
}
}
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);
}
}
3.3、Class获取资源
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);
}
}
<!—精确查询,是相对于当前应用-->
<url-pattern>/servlet/testPath</url-pattern>
<!—模糊查询,表示匹配servlet目录下的所有文件或请求。/表示匹配所有-->
<url-pattern>/servlet/</url-pattern>
<!—模糊查询,表示匹配所有后缀名为do的文件或请求-->
<url-pattern>*.do</url-pattern
五、总结
评论区