【ServletConfig、ServletContext】使用ServletContext、类加载器读取资源文件

1、在实际开发中有一些参数不适合在servlet中写死,这时候就可以在servlet中的init中配置。

<init-param>
   	 <param-name>username</param-name>
    	<param-value>jingtian</param-value>
    </init-param>

2、在dopost/doget方法中调用this.getServletConfig()分析

 查看GenericServlet代码

private transient ServletConfig config;

@Override
    public ServletConfig getServletConfig() {
        return config;
    }

 @Override
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }

使用ServletConfig得到参数

this.getServletConfig().getInitParameter("username");

得到所有参数:
Enumeration e= this.getInitParameterNames();  
        while(e.hasMoreElements()){  
            String a=(String) e.nextElement();  
            String v=this.getServletConfig().getInitParameter(a);  
            System.out.println(a+"="+v);  
        }  



3、web容器在启动的时候,他会为每一个web应用程序创建一个ServletContext对象,它代表当前的web应用。因此ServletContext对象也被称为context域对象。

 <context-param>
  	<param-name>data</param-name>
  	<param-value>xxxx</param-value>
  </context-param>

4、ServletContext的两种得到方式:

ServletContext context=this.getServletConfig().getServletContext();
		
		context=this.getServletContext();

5、ServletContext实现请求转发

context.getRequestDispatcher("/index.html").forward(request, response);

6、ServletContext实现多个servlet之间通讯

setAttribute()
getAttribute()

7、使用ServletContext读取资源文件

package com.xiaozhi.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;

import com.xiaozhi.dao.UserDao;

public class ServletDemo extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		UserDao dao=new UserDao();
		dao.test2();
		
	}

	//下面讲解的是Servlet读取资源文件
	public void test3() throws FileNotFoundException, IOException
	{
		//FileINputStream使用相对路径读取classes下的db.properties
		FileInputStream fileInputStream=new FileInputStream("../webapps/HelloJavaWeb/WEB-INF/classes/db.properties");
		Properties properties=new Properties();
		properties.load(fileInputStream);
		
		System.out.println(properties.getProperty("username"));
		System.out.println(properties.getProperty("password"));
	}

	public void test2() throws FileNotFoundException, IOException
	{
		//FileINputStream使用绝对路径读取classes下的db.properties
		String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
		FileInputStream fileInputStream=new FileInputStream(path);
		Properties properties=new Properties();
		properties.load(fileInputStream);
		
		System.out.println(properties.getProperty("username"));
		System.out.println(properties.getProperty("password"));
	}

	public void test1() throws IOException
	{
		//使用ServletContext读取classes下的db.properties
		InputStream inputStream=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
		Properties properties=new Properties();
		properties.load(inputStream);
		
		System.out.println(properties.getProperty("username"));
		System.out.println(properties.getProperty("password"));
	}

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

	}

}
8、使用类加载器读取资源文件

package com.xiaozhi.dao;

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

public class UserDao
{

	
	private static Properties properties=new Properties();
	static{
		//类加载器以类的方式读取资源文件,资源文件会永驻内存,如果资源文件发生改变,服务器不重启,则不能实时更新
		//UserDao.class也是永驻内存
		InputStream inputStream=UserDao.class.getClassLoader().getResourceAsStream("db.properties");
		try
		{
			properties.load(inputStream);
		} catch (IOException e)
		{
			throw new ExceptionInInitializerError(e);
		}
		
	}
	
	//资源资源发生改变,实时更新
	public void test2() throws IOException 
	{
		//使用类加载器去读资源文件的位置
		String path=UserDao.class.getClassLoader().getResource("db.properties").getPath();
		FileInputStream inputStream=new FileInputStream(path);
		Properties properties=new Properties();
		properties.load(inputStream);
		
		System.out.println(properties.getProperty("username"));
		System.out.println(properties.getProperty("password"));
	}
	
	//使用类加载器读取资源文件
	public void test1() throws IOException 
	{
		//classe下的所有类都使用一个加载器,这个类加载器可以直接加载classes下的文件
		InputStream inputStream=UserDao.class.getClassLoader().getResourceAsStream("db.properties");
		Properties properties=new Properties();
		properties.load(inputStream);
		
		System.out.println(properties.getProperty("username"));
		System.out.println(properties.getProperty("password"));
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值