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