javaweb 路径总结
**路径分为写法两种情况:**
1.客户端路径 ==> 给浏览器用的路径
<form action="/PathTest/AServlet" >
<a href="/PathTest/AServlet" >
<img src="PathTest/AServlet" >
response.sendRedirect("/PathTest/AServlet")
Refresh:3;url=/PathTest/AServlet
路径写法:
带"/" : "/" ==> 相对于 主机.
例如: 表单所在页面路径为==> http://localhost:8080/PathTest/login.jsp ==> "/" 代表http://localhost:8080/
不带"/":(开发不要出现不带"/"的情况).代表从当前目录找.
例如: 表单所在页面路径为==> http://localhost:8080/PathTest/login.jsp ==> 代表 http://localhost:8080/PathTest/
2.服务器端路径
<url-pattern> /AServlet ==> http://localhost:8080/PathTest/AServlet
request.getRequestDispatcher("/AServlet") ==> http://localhost:8080/PathTest/AServlet
路径写法:
"/": 相对于项目. "/"==>http://localhost:8080/PathTest/
jsp获取根路径
${pageContext.request.contextPath} EL 获取web应用的根路径
sevlet中获取资源
public class AServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("request.getRequestURL()==> " + request.getRequestURL());
// http://localhost:8080/PathTest/AServlet
System.out.println("request.getRequestURI()==>" + request.getRequestURI());
// /PathTest/AServlet
System.out.println("request.getScheme()==>" + request.getScheme());
// http
System.out.println("request.getServerName()==>" + request.getServerName());
// localhost
System.out.println("request.getServerPort()==> " + request.getServerPort());
// 8080
System.out.println("request.getContextPath()==> " + request.getContextPath());
// /PathTest
System.out.println("request.getServletPath()==> " + request.getServletPath());
// /AServlet
System.out.println("request.getServletContext().getRealPath('/')=web项目的根路径=> " + request.getServletContext().getRealPath("/"));
//web项目的根路径 D:\software\Tomcat7\PathTest\
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/"+ request.getServletPath();
System.out.println("basePath==>"+basePath);
// basePath==>http://localhost:8080/PathTest//AServlet
System.out.println("-=-=-=-=-=-=-=-=-=--==-=-=-==-=-=-=--=-=-=-=-=-");
// --------getServletContext()------------------------------------
// InputStream getServletContext().getResourceAsStream(String path) ==>==>获取字节输入流
/*
// getServletContext().getResource(String path) ==>URL.getPath()==>获取绝对路径
String path =this.getServletContext().getResource("/WEB-INF/test.xml").getPath();
response.getWriter().print(path);
*/
// 用下面的!!
// String getRealPath(String path)==>获取绝对路径 //获得lib目录下的资源
String path1 = getServletContext().getRealPath("/WEB-INF/lib/test.xml");
System.out.println(path1);
// 获得src下的资源==> 获得classes目录下的资源
getServletContext().getRealPath("/WEB-INF/classes/test.xml");
// 获得cn.itcast.servlet.servlet_context包下的资源
getServletContext().getRealPath("/WEB-INF/classes/cn/servlet/test.xml");
System.out.println("-=-=-=-=-=-=-=--==-=-=-==-=-=-=--=-=-=-=-=-");
//java通用的方法
// --------使用getClass()-----------------------------------------
// 如果获得的是包下的,那么太麻烦了.
// 一:使用getClass().getResourceAsStream方法,相对路径分为两种情况
// 1: 加"/" ==> 相对的是classes目录
// 2: 不加"/" ==> 相对的是本类当前目录
InputStream is = this.getClass().getResourceAsStream("test.xml");
System.out.println(is);
// 二:使用this.getClass().getClassLoader().getResourceAsStream("");获取, 只有一个相对路径 ==> 就是相对于 classes目录
InputStream is2 = this.getClass().getClassLoader().getResourceAsStream("test.xml");
System.out.println(is2);
// 注意: 使用类和类加载器加载资源文件时
// 1 jvm运行期间 类只加载一次. 但是使用下面的代码可以解决这个问题.
String path = this.getClass().getClassLoader().getResource("test.xml").getPath();
System.out.println(path);
// 2 getClassLoader()原本是用来加载.class文件的, 所以缓存设计的很小.不要用他加载一些别较大的资源.
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}