Package CN. servlet. demo1;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. printwriter;
Import java. util. properties;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
// Servlet obtains the resource file
Public class servlet4 extends httpservlet {
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Obtain the resource file
Inputstream in = This. getservletcontext (). getresourceasstream ("WEB-INF" + file. Separator + "classes" + file. Separator + "DB. properties ");
// Directly access the files in the directory under Servlet
// Inputstream in = This. getservletcontext (). getresourceasstream ("file" + file. Separator + "DB. properties ");
// All properties files must be read using properties.
Properties pro = new properties ();
Pro. Load (in );
// Obtain the content in the resource file
String url = pro. getproperty ("url ");
String username = pro. getproperty ("username ");
String Password = pro. getproperty ("password ");
Printwriter out = response. getwriter ();
Out. println (URL + username + password );
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
// Another method for accessing resource files
Public void test () throws ioexception {
String Path = This. getservletcontext (). getrealpath ("/WEB-INF/classes/DB. properties ");
Fileinputstream file = new fileinputstream (PATH );
Properties pro = new properties ();
Pro. Load (File );
String url = pro. getproperty ("url ");
String username = pro. getproperty ("username ");
String Password = pro. getproperty ("password ");
System. Out. println (URL + username + password );
}
}
Common class to obtain resource files
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. util. properties;
Import org. JUnit. test;
Public class demo {
@ Test
Public void show () throws ioexception {
// Not updated
// Inputstream in = demo. class. getclassloader (). getresourceasstream (".. "+ file. separator + ".. "+ file. separator + "file/file. properties ");
// Properties pro = new properties ();
// Pro. Load (in );
// String url = pro. getproperty ("url ");
// System. Out. println (URL );
// Updated
// String Path = demo. class. getclassloader (). getresource (".. "+ file. separator + ".. "+ file. separator + "file/file. properties "). getpath ();
// Properties pro = new properties ();
// Fileinputstream in = new fileinputstream (PATH );
// Pro. Load (in );
// String url = pro. getproperty ("url ");
// System. Out. println (URL );
// DB. properties must be under the WEB-INF \ Classes directory for direct access.
// String Path = demo. Class. getclassloader (). getresource ("DB. properties"). getpath ();
// Properties pro = new properties ();
// Fileinputstream in = new fileinputstream (PATH );
// Pro. Load (in );
// String url = pro. getproperty ("url ");
// System. Out. println (URL );
Inputstream in = demo. Class. getclassloader (). getresourceasstream ("DB. properties ");
Properties pro = new properties ();
Pro. Load (in );
String name = pro. getproperty ("url ");
System. Out. println (name );
}
}