绝对路径:
第一种:
</body>
<script>
// <!-- JS文件中绝对路径的获取 -->
action="${pageContext.request.contextPath}/相对路径"
</script>
</html>
第二种:利用ServletContext共享域获取绝对路径
public class ServletContextDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = this.getServletContext();
//获取应用的访问虚拟目录 aaa
//根据虚拟目录获取应用部署的磁盘绝对路径 D:\review\out\artifacts\review_war_exploded\
String realPath = context.getRealPath("/");
System.out.println(realPath);
}
虚拟路径:
第一种:利用ServletContext共享域获取虚拟路径
public class ServletContextDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = this.getServletContext();
//获取应用的访问虚拟目录 aaa
String contextPath = context.getContextPath();
System.out.println(contextPath);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
第二种:利用请求来获取(Request)
@WebServlet("/ServletDemo01")
public class ServletDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取虚拟目录名称 getContextPath() /aaa
String contextPath = req.getContextPath();
System.out.println(contextPath);