1.
服务器发出的请求 :
"/"相对于应用的根目录来说
服务器发出的请求 "/"相对于webapps根目录来说 ->//转发
// "/Demo4_2", 目前只有转发直接 " /文件名称"
request.getRequestDispatcher("/Demo4_2").forward(request, response);
//重定向,重定向的资源的请求由浏览器发出response.sendRedirect("/W06/Demo4_2");
//超链接也是浏览器发出
hrefresponse.getWriter().write(""+ ""+ "");
//form 中的action
2.找文件的对应相对路径
javaweb 和java下不一样 web下没有bin,有class(服务器tomcat下的WEB_INF-->classess文件夹)
1)、javaWeb下:“/” 相对于/WEB-INF/classes来说(工程中 在 webContent 例如:/upload:------>webContent / upload)
2)、普通的java: "/" 相对于bin目录来说
3.动态获取路径
(javaweb 下 和 java下:)
String path = ResourceDemo2.class.getResource("/db.properties").getPath();
//该方法中, “/”相对于应用的真实路径来说(tomcat中)
this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
//直接获取文件资源的输入流
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//FileInputStream in = new FileInputStream(path);
Properties pro = new Properties();
pro.load(in);
System.out.println(pro.getProperty("user"));
4.
//放在某一个包中不需要加 “/” ("/相对于bin来说")
//有“/”相对于bin,没有“/”相对于class来说
InputStream in = DbcpTest.class.getResourceAsStream("db.properties");