Servlet获取资源

一、ServletContext

>Servlet上下文,每个web工程都只有一个ServletContext对象,也就是不管在哪个Servlet里面,获取到的这个类的对象都是同一个

如何得到对象?

//获取对象
ServletContext context = getServletContext();

有什么作用?

1、获取全局配置参数

2、获取web工程中的资源

3、存取数据,servlet间共享数据

获取全局配设置参数

<!-- 全局参数:哪个servlet都可以拿,ServletContext -->
<context-param>
    <param-name>address</param-name>
    <param-value>北京</param-value>
</context-param>

获取全局参数

ServletContext context = getServletContext();
String address = context.getInitParameter("address");
System.out.println("address = " + address);

获取web应用中的的资源
>获取资源在tomcat里面的绝对路径

    context.getRealPath("")           //这里得到的是项目在tomcat里面的根目录。

    D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo\

   String path = context.getRealPath("file/config.properties");

   D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo\file\config.properties

ServletContext context = getServletContext();
String path = context.getRealPath("file/config.properties");
System.out.println(path);
		
Properties properties = new Properties();
InputStream is =new FileInputStream(path);
properties.load(is);
		
String name = properties.getProperty("name");
System.out.println("name = " + name);

>getResourceAsStream  获取资源流对象

   直接给相对路径,然后获取流对象

try {
    ServletContext context = getServletContext();

	//获取web工程下的资源,转化成流对象。前面隐藏当前工程的根目录
	/*
	* 相对路径(有参照物)相对谁?
        * 		工程tomcat里面的路径
	* 
	* a路径----工程在tomcat里面的根目录
	*          D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo
	* b路径----
	*          file\config.properties
	* 
	*          D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo\file\config.properties
	*  绝对路径(没有参照物)
	*      D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo\file\config.properties
	*/
			
	InputStream is = context.getResourceAsStream("file/config.properties");
	Properties properties = new Properties();
	properties.load(is);

	String name = properties.getProperty("name");

	System.out.println("name2 = " + name);
} catch (Exception e) {
	e.printStackTrace();
}

二、通过ClassLoader获取web下的资源

try {
	Properties properties = new Properties();
			
	/*
	* ServletContext
	* a路径----工程在tomcat里面的路径
	*      D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo
	*      
	* ClassLoader
	* a路径----D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo\WEB-INF\classes
	*  
	* 默认的ClassLoader的路径是上面这个路径,我们必须得回到ServletContextDemo这个目录下,才能得到file目录
	* 如何回到上一级目录呢?
	* ../../ ---- D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo
	* ../../file/config.properties  ----D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo\file\config.properties
	* 
	* b路径----D:\apache-tomcat-8.5.15\wtpwebapps\ServletContextDemo\file\config.properties
	*/

	InputStream is = this.getClass().getClassLoader().getResourceAsStream("../../file/config.properties");
	properties.load(is);

	String name = properties.getProperty("name");
	System.out.print("name3 = " + name);
} catch (Exception e) {
	e.printStackTrace();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值