Java: 使用ServletContext获取资源路径

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 它得到的是有盘符的路径:F:/xxx/xxx/xx/
         * F:\JavaEE20140508\apache-tomcat-7.0.42\webapps\day09_2\index.jsp
         */
        String path = this.getServletContext().getRealPath("/index.jsp");
        System.out.println(path);
        
        /*
         * 获取资源的路径后,再创建出输入流对象!
         */
        InputStream input = this.getServletContext().getResourceAsStream("/index.jsp");
        
        /*
         * 获取当前路径下所有资源的路径!
         */
        Set paths = this.getServletContext().getResourcePaths("/WEB-INF");
        System.out.println(paths);
    }

你可能感兴趣的:(Java: 使用ServletContext获取资源路径)