路径问题总结
一丶与路径相关的操作
①超链接
②表单
③包含
④重定向
⑤<url-pattern>
⑥ServletContext获取资源
⑦Class获取资源
⑧ClassLoader获取资源
二丶客户端路径
超链接、表单、重定向都是客户端路径,客户端路径可以分为三种方式:
①绝对路径;
②以“/”开头的相对路径;
③不以“/”开头的相对路径;
例如:http://localhost:8080/Web_PathProblem/index.html中的超链接和表单如下:
绝对路径:<a href="http://localhost:8080/Web_PathProblem/index.html">链接1</a><br/>
以“/”开头的相对路径:<a href="/Web_PathProblem/index.html">链接2</a><br/>
相对路径:<a href="index.html">链接3</a><br/>
<hr/>
绝对路径:
<form action="http://localhost:8080/Web_PathProblem/index.html">
<input type="submit"value="表单1"/>
<hr/>
</form>
相对路径1:
<form action="/Web_PathProblem/index.html">
<input type="submit"value="表单2"/>
</form>
<hr/>
相对路径2:
<form action="index.html">
<input type="submit"value="表单3"/>
</form>
点击其中任何的连接都会跳转到指定的界面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>welcome</h2>
</body>
</html>
重定向带"/"的相对路径:
public class AServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("/Web_PathProblem/index.html");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
重定向不带”/”的相对路径:
public class AServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("index.html");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
三丶服务器端路径
服务器端路径必须是相对路径,不能是绝对路径。但相对路径有两种形式:
①以“/”开头;
②不以“/”开头;
其中请求转发、请求包含都是服务器端路径,服务器端路径与客户端路径的区别是:
客户端路径以“/”开头:相对当前主机;
服务器端路径以“/”开头:相对当前应用(项目名称);
转发1代码:
publicclass AServlet extends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/BServlet").forward(request, response);
}
}
假设访问AServlet的路径为:http://localhost:8080/Web_PathProblem/servlet/AServlet
因为路径以“/”开头,所以相对当前应用,即http://localhost:8080/Web_PathProblem/BServlet。
转发2代码:
publicclass AServlet extends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("BServlet").forward(request, response);
}
}
假设访问AServlet的路径为:http://localhost:8080/Web_PathProblem/servlet/AServlet
因为路径不以“/”开头,所以相对当前应用,即http://localhost:8080/Web_PathProblem/servlet/BServlet。
四丶路径
<url-pattern>必须使用“/”开头,并且相对的是当前应用。
五丶ServletContext获取资源
必须是相对路径,可以“/”开头,也可以不使用“/”开头,但无论是否使用“/”开头都是相对当前应用路径。
例如在AServlet中获取资源,AServlet的路径路径为:http://localhost:8080/Web_PathProblem/servlet/AServlet:
publicclass AServlet extends HttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path1 = this.getServletContext().getRealPath("Demo.txt");
String path2 = this.getServletContext().getRealPath("/Demo.txt");
System.out.println(path1);
System.out.println(path2);
}
}
六丶Class获取资源
Class获取资源必须是相对路径,可以“/”开头,也可以不使用“/”开头。
import java.io.InputStream;
public class Demo {
publicvoid fun1() {
InputStream in = Demo.class.getResourceAsStream("/a.txt");
}
publicvoid fun2() {
InputStream in = Demo.class.getResourceAsStream("a.txt");
}
}
其中fun1()方法获取资源时以“/”开头,那么相对的是当前类路径,即/Web_PathProblem/WEB-INF/classes/a.txt文件;
其中fun2()方法获取资源时没有以“/”开头,那么相对当前Demo.class所在路径,因为Demo类在cn.itcast包下,所以资源路径为:/Web_PathProblem/WEB-INF/classes/com/demo/servlet/a.txt。
七丶ClassLoader获取资源
ClassLoader获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。但无论是否以“/”开头,资源都是相对当前类路径。
publicclass Demo {
publicvoid fun1() {
InputStream in = Demo.class.getClassLoader().getResourceAsStream("/a.txt");
}
publicvoid fun2() {
InputStream in = Demo.class.getClassLoader().getResourceAsStream("a.txt");
}
}
fun1()和fun2()方法的资源都是相对类路径,即classes目录,即/Web_PathProblem/WEB-INF/classes/a.txt