Methods for obtaining resource files by using Servlet and common classes

Source: Internet
Author: User
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 );
}
}













Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.