servlet加载资源两种方式-内外(初始化参数).properties文件

  在servlet中加载资源很方便,通过servletContext,这个是web服务器加载完web项目返回的参数之一,还有servletConfig,得到web项目一些配置信息,request和response,等等,我们现在用的这个servletContext也叫做Context域,一个web一共有四个域,分别是Context域,Session域,request域和response域,context域指的是整个应用程序,域指的就是范围。

  1.那么servletContext加载资源通过getResourceAsOutputStream(),来返回一个流,通过这个流我们读取资源文件。我们这里读取(.properties)文件。

  请注意,这里是在Servlet中读取,而我的properties文件存在与这里

  看代码

1         ServletContext context = this.getServletContext();
2         InputStream in = context.getResourceAsStream("/WEB-INF/classes/jdbc.properties");

  这样我们就能获得这文件的一个字节输入流。然后我们通过javaAPI中的Properties这个类来解析

 1         ServletContext context = this.getServletContext();
 2         InputStream in = context.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
 3         
 4         /*
 5          * 下面是模版代码
 6          * */
 7         Properties prop = new Properties();
 8         prop.load(in);
 9         String url = (String) prop.getProperty("url");
10         System.out.println("打印:"+ url);

  用Properties这个类,要注意,这里的文件不能太大,为什么呢?因为这个类把这个文件加载到内存里,为了防止内存的溢出,所以它是有大小限制的,这里要注意。

  2.用getRealPath()方法来获取这个文件的绝对路径,然后通过FileInputStream来获得这个字节输入流。用这个方法有一个好处,就是我们能通过这个path来获得

  这个文件的文件名称。

 1         ServletContext context = this.getServletContext();
 2         String path = context.getRealPath("/WEB-INF/classes/jdbc.properties");
 3         
 4         FileInputStream in = new FileInputStream(path);
 5         
 6         /*
 7          * 下面是模版代码
 8          * */
 9         Properties prop = new Properties();
10         prop.load(in);
11         String url = (String) prop.getProperty("url");
12         System.out.println("打印:"+ url);
13         

  下面是不在servlet里面,也就是说没有servletContext这个对象,我们来读取文件

  这里我们同过类的装载器来实现。

 1 //PersonDao.class.getClassLoader()这个就是类的装载器,我得到了这个类的装载器
 2         //就是得到了所有src里面的东西
 3         InputStream in = PersonDao.class.getClassLoader().getResourceAsStream("jdbc.properties");
 4         /*
 5          * 下面是模版代码
 6          * */
 7         Properties prop = new Properties();
 8         prop.load(in);
 9         String url = (String) prop.getProperty("url");
10         System.out.println("打印:"+ url);

 这里的路径为什么直接就写文件名称,因为这个PersonDao.class.getClassLoader()就是类的加载器,就代表着classes,所以我们直接加载就行。

  说一下,这里用Properties这个类,如果说你修改了jdbc.properties资源文件,比如里面的url的值被你修改了,而你的服务器没有重新启动,那么你在次执行的时候,还是原来的值

 这个是因为src,也就是这个类只加载一次,当你要访问这个文件的时候,虚拟机一看,我内存里有,我就直接给你拿,他不会重新给你加载。

  如果你非要得到这个修改后的值的话,那你就要通过PersonDao.class.getResource(“”)方法,就不要去用类装载器了,这个方法会通过你给的资源,返回给你一个URL路径,我们通过这个路径用FilInputStream得到一个字节输入流,这样确保我们每次拿到的都是一个新的流,得到的才是一个新的数据。

 1         String path = PersonDao.class.getResource("jdbc.properties").getPath();
 2         FileInputStream in = new FileInputStream(path);
 3         
 4         /*
 5          * 下面是模版代码
 6          * */
 7         Properties prop = new Properties();
 8         prop.load(in);
 9         String urls = (String) prop.getProperty("url");
10         System.out.println("打印:"+ urls);

  

如果有使用请标明来源:http://www.cnblogs.com/duwenlei/
原文地址:https://www.cnblogs.com/duwenlei/p/3489416.html