“/” ==“Web应用\WebRoot\WEB-INF\classes”
“/” == “Web应用”
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//取得当前对象的字节码对象
Class clazz = this.getClass();
//取得当前对象的类加载器
ClassLoader cl = clazz.getClassLoader();
//通过类加载器加载资源文件
InputStream is = cl.getResourceAsStream("WEB-INF/classes/config.properties");
//全路径 物理路径而不是IDE的路径 day06/WEB-INF/classes/config.properties
//InputStream is = cl.getResourceAsStream("WEB-INF/classes/cn/incast/web/config.properties");
//全路径 物理路径而不是IDE的路径 day06/WEB-INF/classes/cn/itcast/web/config.properties
//InputStream is = cl.getResourceAsStream("WEB-INF/config.properties");
//全路径 物理路径而不是IDE的路径 day06/WEB-INF/config.properties
Properties props = new Properties();
props.load(is);
System.out.println(props.getProperty("email"));
/*传统方式
InputStream is = new FileInputStream("src/config.properties");
//全路径 tomcat/bin/src/config.properties
Properties props = new Properties();
props.load(is);
System.out.println(props.getProperty("email"));
*/
/*ServletContext context = this.getServletContext();
InputStream is = context.getResourceAsStream("/doc/config.properties");
//全路径 物理路径而不是IDE的路径 day06/WEB-INF/doc/config.properties
Properties props = new Properties();
props.load(is);
System.out.println(props.getProperty("email"));
*/