绝对路径与虚拟路径的获取

绝对路径:

第一种:

</body>
<script>
    // 
    action="${pageContext.request.contextPath}/相对路径"
</script>
</html>

第二种:

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);
    }

虚拟路径:

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);
    }
}

你可能感兴趣的:(绝对路径与虚拟路径的获取)