How to read resource files in servlet and non-servlet entity classes

Source: Internet
Author: User

It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.

Directory structure for Web projects


reading resource files in the servlet

public class ServletDemo6 extends HttpServlet {

/**

* ServletContext read the resource files in the Web application db.properties

**/

public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

/**

* Get resource Stream object by Servletcontext.getresourceasstream () method

**/

String Path = "/web-inf/classes/db.properties";

String Path = "/web-inf/classes/edu/servlet/db.properties";

String Path = "/db.properties";

getResourceAsStream (Path,response);

/**

* Access to resource stream objects through traditional file streams, but using real paths

**/

String Path = "/web-inf/classes/db.properties"; No, you need to use the real path.

String path = This.getservletcontext (). Getrealpath ("/web-inf/classes/db.properties");

FileInputStream (Path,response);

/**

* Call non-servlet class, get resource file in Userdao

**/

New Userdao (). Update1 ();

New Userdao (). Update2 ();

New Userdao (). Update (This.getservletcontext ());

}

/*

* Read the resource file to note the problem: The following traditional code is not possible, preferably using the Servletcontext.getresourceasstream () method to read

* or after the absolute path of the resource is obtained through the ServletContext Getrealpath, the resource file is read through the traditional stream

*/

private void FileInputStream (String path, httpservletresponse response) throws IOException {

FileInputStream in = new FileInputStream (path);

Properties Pros = new properties ();

Pros.load (in);

String filename = path.substring (path.lastindexof ("\") +1);

String Driver = pros.getproperty ("Driver");

String url = pros.getproperty ("url");

String username = pros.getproperty ("username");

String Password = pros.getproperty ("password");

PrintWriter out = Response.getwriter ();

OUT.PRINTLN ("filename =" +filename);

Out.println ("Driver =" +driver);

Out.println ("url =" +url);

Out.println ("username =" +username);

OUT.PRINTLN ("password =" +password);

}

private void getResourceAsStream (String path, httpservletresponse response) throws IOException {

InputStream in = This.getservletcontext (). getResourceAsStream (path);

Properties Pros = new properties ();

Pros.load (in);

String Driver = pros.getproperty ("Driver");

String url = pros.getproperty ("url");

String username = pros.getproperty ("username");

String Password = pros.getproperty ("password");

PrintWriter out = Response.getwriter ();

Out.println ("Driver =" +driver);

Out.println ("url =" +url);

Out.println ("username =" +username);

OUT.PRINTLN ("password =" +password);

}

}


reading resource files in non-servlet entity classes

public class Userdao {

/**

* This is certainly OK for the program, but it does not conform to the program design idea because the coupling between the DAO layer and the servlet layer increases,

* So should not be used in such a way, so can only use the class loader

**/

public void Update (ServletContext context) throws IOException {

InputStream in = Context.getresourceasstream ("/web-inf/classes/db.properties");

Properties Props = new properties ();

Props.load (in);

System.out.println ("url =" +props.getproperty ("url"));

}

/**

* If the program reading the resource file is not a servlet, you can only read it through the class loader

* If you use a class loader to read a resource file to generate a inputstream stream, there is also a problem, because the class loader will only load once when the service is started.

* So, when the content of Db.properties is updated, the class loader will not read the resource file until Tomcat starts again.

* So printing is still the old information, unless you redeploy the Web application (no need to restart Tomcat)

**/

public void Update1 () throws IOException {

Since the Userdao class and the db.properties are in the same directory, the class loader loads also at the same time as the Userdao classes load db.properties

InputStream in = UserDao.class.getClassLoader (). getResourceAsStream ("db.properties");

Properties Props = new properties ();

Props.load (in);

System.out.println ("url =" +props.getproperty ("url"));

}

/**

* There are two kinds of problems in the synthesis, we should solve this problem:

* Get the location of the resource file through class loading, and then read the resource file data through the traditional way (FileInputStream).

* This allows you to read the updated data

*

*

**/

public void Update2 () throws IOException {

Since the Userdao class and the db.properties are in the same directory, the class loader loads also at the same time as the Userdao classes load db.properties

String path = UserDao.class.getClassLoader (). GetResource ("Db.properties"). GetPath ();

/*

* Print: c:/program%20files/apache%20software%20foundation/tomcat%206.0/webapps/servletdetail/web-inf/classes/ Db.properties

* So this way, if there are special characters such as spaces in the path, the FileNotFoundException exception will be thrown, but the path is valid.

* or you can: accept a path parameter in the method, from the servlet through This.getservletcontext (). Getrealpath ("/web-inf/classes/db.properties") came

*/

SYSTEM.OUT.PRINTLN (path);

FileInputStream in = new FileInputStream (path);

Properties Props = new properties ();

Props.load (in);

System.out.println ("url =" +props.getproperty ("url"));

}


}





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.