javaweb中读取配置文件的方式

读取javaweb中配置文件的方式,一般来说,主要有如下三种:

一:通过ServletContext读取配置文件

二:通过类加载器读取配置文件

三:通过文件的绝对路径,然后通过传统的方式得到配置文件

假设web工程中有如下不同位置的配置文件,我们如何读取呢?

一:通过ServletContext读取配置文件

代理示例如下:

package cn.ccnu.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReadProperties extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//使用ServletContext读取配置文件(使用配置文件的相对路径)
		//注:getResourceAsStream中配置文件的路径的写法为:web工程发布到服务器后所对应的目录,是一个相对路径
		
		//1,读取src目录下的配置文件,因为web工程发布到服务器后的src.properties的相对路径为:/WEB-INF/classes/src.properties
		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/src.properties");
		Properties prop = new Properties();
		prop.load(in);
		String url = prop.getProperty("url");
		System.out.println(url);
		
		//2,读取包cn.ccnu目录下的配置文件,因为web工程发布到服务器后的cnccnu.properties的相对路径为:/WEB-INF/classes/cn/ccnu/cnccnu.properties
		InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/ccnu/cnccnu.properties");
		Properties prop2 = new Properties();
		prop2.load(in2);
		String ur2 = prop2.getProperty("url");
		System.out.println(ur2);
				
		//3,读取webRoot目录下的配置文件,因为web工程发布到服务器后的webroot.properties的相对路径为:/webroot.properties
		InputStream in3 = this.getServletContext().getResourceAsStream("/webroot.properties");
		Properties prop3 = new Properties();
		prop3.load(in3);
		String ur3 = prop3.getProperty("url");
		System.out.println(ur3);
		
		//使用ServletContext读取配置文件(使用配置文件的绝对路径)
		//注:getRealPath中的参数依旧是web工程发布到服务器后所对应的目录,是一个相对路径
		//1,读取src目录下的配置文件
		String path = this.getServletContext().getRealPath("/WEB-INF/classes/src.properties");
		FileInputStream fin = new FileInputStream(path);
		Properties prop4 = new Properties();
		prop4.load(fin);
		String ur4 = prop4.getProperty("url");
		System.out.println(ur4);
		//2,读取包cn.ccnu目录下的配置文件
		String path2 = this.getServletContext().getRealPath("/WEB-INF/classes/cn/ccnu/cnccnu.properties");
		FileInputStream fin2 = new FileInputStream(path2);
		Properties prop5 = new Properties();
		prop5.load(fin2);
		String ur5 = prop5.getProperty("url");
		System.out.println(ur5);
		//3,读取webRoot目录下的配置文件
		String path3 = this.getServletContext().getRealPath("/webroot.properties");
		FileInputStream fin3 = new FileInputStream(path3);
		Properties prop6 = new Properties();
		prop6.load(fin3);
		String ur6 = prop6.getProperty("url");
		System.out.println(ur6);
	}

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

}

二:通过类加载器读取资源文件

示例代码如下:
//通过类加载器读取配置文件(相对路径,只能读取src下的配置文件)
		//1,读取src目录下的配置文件   注意,不要加/
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("src.properties");
		Properties prop = new Properties();
		prop.load(in);
		String url = prop.getProperty("url");
		System.out.println(url);
		//2,读取包cn.ccnu目录下的配置文件
		InputStream in2 = this.getClass().getClassLoader().getResourceAsStream("cn/ccnu/cnccnu.properties");
		Properties prop2 = new Properties();
		prop2.load(in2);
		String ur2 = prop2.getProperty("url");
		System.out.println(ur2);
		
		//通过类加载器读取配置文件(绝对路径)
		//1,读取src目录下的配置文件
		String path = this.getClass().getClassLoader().getResource("src.properties").getPath();
		FileInputStream fin1 = new FileInputStream(path);
		Properties prop3 = new Properties();
		prop3.load(fin1);
		String ur3 = prop3.getProperty("url");
		System.out.println(ur3);
		//2,读取包cn.ccnu目录下的配置文件
		String path2 = this.getClass().getClassLoader().getResource("cn/ccnu/cnccnu.properties").getPath();
		FileInputStream fin2 = new FileInputStream(path2);
		Properties prop4 = new Properties();
		prop4.load(fin2);
		String ur4 = prop4.getProperty("url");
		System.out.println(ur4);


第三种方式,通过得到配置文件的绝对路径,然后得到配置文件,和上面讲解的差不多。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值