Web项目下试验ServletContext读取配置文件状态码500出错

package com.servlet.servletcontext;

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 ServletContext01 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 创建Properties实例
		Properties properties = new Properties();
		// load()方法加载配置文件输入流
		InputStream is = new FileInputStream("classes/config.properties");
		properties.load(is);
		// getProperty(String key)获得相应的值
		String address = properties.getProperty("address");
		
		System.out.println("ServletContext01: address=" + address);
		/*//1. 创建属性对象
				Properties properties = new Properties();
				
				//2. 指定载入的数据源
				
				 * 此处,如果想获取web工程下的资源,用普通的FileInputStream 写法是不OK 的。
				 * 因为路径不对了。  这里相对的路径,其实是根据jre来确定的。 但是我们这是一个web工程,
				 * jre 后面会由tomcat管理,所以这里真正相对的路径是 tomcat里面的bin目录
				 
				InputStream is = new FileInputStream("../../../config.properties");
				properties.load(is);
				
				//3. 获取name属性的值
				String address = properties.getProperty("address");
			
				System.out.println("address="+address);*/
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

猜你喜欢

转载自blog.csdn.net/leoBETT/article/details/81571347