Reading of resource files in servlet
Method One: Get the data flow of the resource file
This . Getservletcontext (); = Context.getresourceasstream ("/person.properties"); New Properties (); Pt.load (is); System.out.println (Pt.getproperty ("name"));
Method Two: Get the absolute path of the resource file, then take advantage of the FileInputStream, and the difference is that here you can get the file name to manipulate
This . Getservletcontext (); = Context.getrealpath ("web-inf/classes/person.properties"); System.out.println (Realpath); = Realpath.substring (realpath.lastindexof ("\ \") +1); SYSTEM.OUT.PRINTLN (filename); New FileInputStream (Realpath); New Properties (); Pt.load (FIS); System.out.println (Pt.getproperty ("name"));
Get the resource profile in the normal class because there are no ServletContext objects in the normal class, so use the ClassLoader
URL url = Persondao. class. getClassLoader (). GetResource ("com/baowei/servlet/person.properties"); = Url.getpath (); SYSTEM.OUT.PRINTLN (filepath); New FileInputStream (filepath); New Properties (); pt.load (FIS); System.out.println (Pt.getproperty ("name")); System.out.println (Pt.getproperty ("age"));
Reading of resource files under Web Engineering