JavaWeb下的路径问题
2018-06-03 本文已影响1人
幻影翔
目标资源是给谁使用的。( target.html)
- 思考: 目标资源是给谁使用的。
给服务器使用的: / 表示在当前web应用的根目录(webRoot下)
给浏览器使用的: / 表示在webapps的根目录下
1.转发
request.getRequestDispatcher("/target.html").forward(request, response);
2.请求重定向
response.sendRedirect("/day11/target.html");
3.html页面的超连接href
response.getWriter().write("<html><body><a href='/day11/target.html'>超链接</a></body></html>");
4.html页面中的form提交地址
response.getWriter().write("<html><body><form action='/day11/target.html'><input type='submit'/></form></body></html>");
. 代表java命令运行目录
在web项目中, . 代表在tomcat/bin目录下开始,所以不能使用这种相对路径。
使用web应用下加载资源文件的方法
-
ServletContext.getRealPath("路径")读取,返回资源文件的绝对路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties")
-
ServletContext.getResourceAsStream("路径")
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");