关于Java路径问题的个人总结

客户端路径:超链接、表单、重定向

以 “ / ” 开头,相当于主机 http://localhost:8080 强烈建议使用“/”开头的路径

不以 “ / ” 开头,相当于当前页面的路径http://localhost:8080/hello1/pages/index.html

重定向不可以定向到WEB-INF下的页面,因为客户端不可以直接访问WEB-INF下面的路径,只有转发,包含可以,因为他们是服务端的路径

服务端路径:转发,包含、<url-pattern>、getServletContext().getRealPath("")、

类名 . class . getResourAsStream("") 、类名 . class . getClassLoader().getResourAsStream()

转发,包含不论是否以 “ / ” 开头,都相当于当前应用 即http://localhost:8080/JavaPro/BServlet

<url-pattern>必须以 “ / ” 开头,相当于当前应用

getServletContext().getRealPath("")无论是否使用“/”开头都是相对当前应用。

类名 . class . getResourAsStream("") 以“/”开头,那么相对的是当前类路径/WEB-INF/classes

不以 “ / ” 开头相当于当前类的字节码文件所在的路径(包含包名)

类名 . class . getClassLoader().getResourAsStream()无论是否以“/”开头,资源都是相对当前类路径。/WEB-INF/classes

下面关于IO流的路径问题

InputStream is = null;
Properties p=new Properties();
        /**
         * 使用class变量的getResourceAsStream()方法
         * 资源文件和类文件在不在同一个目录都可以
         * 从包名开始写/com/sue/testproperity/config.properties
         */
    is=Demo01.class.getResourceAsStream("/com/sue/testproperity/config.properties");
        //使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法。
 is=Demo01.class.getClassLoader().getResourceAsStream("com/sue/testproperity/config.properties");
//使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 
 
is=ClassLoader.getSystemResourceAsStream("com/sue/testproperity/config.properties");
 InputStream in4 = Test4.class.getClassLoader().getResource("com/qls/counter/f.properties").openStream();
InputStream in5 = Test5.class.getClassLoader().getResource("com/qls/counter/f.properties").openConnection().getInputStream();
//Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法  
        InputStream in = context.getResourceAsStream(path)

猜你喜欢

转载自blog.csdn.net/wangshulang7262/article/details/81234371