在servlet和非servlet實體類中如何讀取資源檔

來源:互聯網
上載者:User

IT程式員開發必備-各類資源下載清單,史上最全IT資源,個人收藏總結。

web項目的目錄結構

 

在Servlet中讀取資源檔

public class ServletDemo6 extends HttpServlet {

   /**

    * ServletContext讀取 web應用中的資源檔db.properties

    **/

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

/**

 * 通過servletContext.getResourceAsStream()方式得到資源流對象

 **/

//String path = "/WEB-INF/classes/db.properties";

//String path = "/WEB-INF/classes/edu/servlet/db.properties";

//String path = "/db.properties";

   // getResourceAsStream(path,response);

/**

 * 通過傳統的檔案流取得資源流對象,但需要使用真實路徑 

 **/

//String path = "/WEB-INF/classes/db.properties"; //不行,需要使用真實路徑

//String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");

//FileInputStream(path,response);

/**

 * 調用非Servlet類 ,在UserDao中擷取資源檔 

 **/

//new UserDao().update1();

 new UserDao().update2();

//new UserDao().update(this.getServletContext());

}

    

/*

 * 讀取資源檔需要注意的問題:下面傳統代碼不可行,最好採用ServletContext.getResourceAsStream()方法去讀

 * 或者通過ServletContext的getRealPath得到資源的絕對路徑後,在通過傳統流讀取資源檔

 */

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);

    }

}


在非Servlet實體類中讀取資源檔

public class UserDao {

 /**

 * 這樣對於程式而言,當然是可以,但是不符合程式設計思想,因為Dao層和Servlet層之間的耦合度增大了 ,

 * 所以不應該用這樣方式,所以只能使用類裝載器了

 **/

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"));

}

    /**

     * 如果讀取資源檔的程式不是servlet的話, 就只能通過類裝載器去讀了

     * 如果使用類裝載器讀取資源檔產生InputStream流的話,也有問題,因為類裝載器只會在服務啟動的時候裝載一次,

     *    所以,當更新了db.properties的內容後,在tomcat再次啟動之前,類裝載器是不會在讀取資源檔的,

     *    所以列印的仍然是舊資訊,除非重新部署web應用(不用重啟tomcat)

     **/

public void update1() throws IOException {

//因為userdao類和db.properties是在同一目錄下,所以類裝載器在裝載UserDao類的同時,也裝載了db.properties

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

Properties props = new Properties();

props.load(in);

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

}

   /**

    * 綜合上面出現了兩種問題,我們應該這樣解決:

    *  通過 類裝載的方式得到資源檔的位置,再通過傳統方式(FileInputStream)讀取資源檔的資料,

    *   這樣可以讀取到更新後的資料

    *   

    * 

    **/

public void update2() throws IOException {

//因為userdao類和db.properties是在同一目錄下,所以類裝載器在裝載UserDao類的同時,也裝載了db.properties

String path = UserDao.class.getClassLoader().getResource("db.properties").getPath();

/*

 * 列印:C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/ServletDetail/WEB-INF/classes/db.properties

 * 所以這種方式,如果路徑中有空格等特殊字元的話,則會拋出FileNotFoundException異常,但路徑合法的話則可以。

 * 或者可以這樣:在該方法中接受一個path參數,從servlet中通過this.getServletContext().getRealPath("/WEB-INF/classes/db.properties")傳來

 */

System.out.println(path);

FileInputStream in = new FileInputStream(path);

Properties props = new Properties();

props.load(in);

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

}


}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.