servlet中servletContext的五大作用(五)

1.    获取web的上下文路径

2.    获取全局的参数

3.    作为域对象使用

4.    请求转发

5.    读取web项目的资源文件

package day10.about_servletcontext.get_resource;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 【context对象的作用-5】:读取web项目的资源文件
 *  
 *    	
 *	@author mzy 	
 */
public class ContextDemo05 extends HttpServlet {

	private static final long serialVersionUID = -8190742959528279701L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		/**
		 * 复习:如何读取properties文件?
		 */
		// 此相对路径出错
		/*
		 *  此相对路径出错:"./src/news.properties"
		 *  	当前路径应该是相对于java运行的目录
		 */
		// File f = new File(".");
		// D:UsersAdministratorAppDataLocalMyEclipse 2017 CI.
		// System.out.println(f.getAbsolutePath());
		/*
		 * 通过以上的测试,我得出当前的相对路径在:D:UsersAdministratorAppDataLocalMyEclipse 2017 CI
		 * 我尝试在此目录下新建了一个src文件夹,在下面新建了一个news.properties
		 * 测试就通过了,说明当前的相对路径和我认为的相对路径并不相同;
		 *
		 *
		 * 所以建议我们在web项目下不要使用相对路径!
		 * 
		 * 所以在web下提供了三种读取web项目的资源文件:
		 * java.lang.String getRealPath(java.lang.String path)
		 * java.InputStream getResourceAsStream(java.lang.String path)
		 * java.net.URL getResource(java.lang.String path)
		 * 
		 */
		/*
		// FileInputStream in = new FileInputStream("./src/news.properties");
		// 1) 使用properties对象
		Properties prop = new Properties();
		// 2) 使用load方法加载properties文件
		prop.load(in);
		// 3) 通过getProperty() 获取内容
		System.out.println(prop.getProperty("name"));
		System.out.println(prop.getProperty("password"));
		*/
		
		
		/*
		 * 因为src路径只是我们开发的时候才会用的路径:
		 * 而真实的环境下,我们的项目应该是跑在tomcat环境下的,
		 * 在tomcat环境下的话,我们的所有class文件都应该存放在
		 * WEB-INF/classes文件夹下的。
		 * 那么我们的news.properties文件存放在我们开发的src文件
		 * 夹中,那么此文件就应该在我们的classes文件夹下。
		 * 
		 * 因为这个动作是在服务器中进行的,所以这个应该是一个服务器
		 * 行为
		 * 
		 * 服务器行为下的绝对路径定位到我们的项目根目录,并非站点的根目录
		 * 所以我们书写的语句如下:
		 * 
		 */
		
		// 获取到当前在网站运行下的真实的路径:
		// 通过 / 返回到根目录下(当前的服务器行为:项目根目录)
		
		// 1. getRealPath()  获取资源文件的真实路径: String path
		// String path = this.getServletContext().getRealPath("/WEB-INF/classes/news.properties");
		// FileInputStream in = new FileInputStream(new File(path));
		
		// 2. getResourceAsStream() 直接获得资源文件的输入流; InputStream
		// InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/news.properties");
		
		// 3. getResource() 获得资源文件的URL 与第一种相似
		URL url = this.getServletContext().getResource("WEB-INF/classes/news.properties");
		String path = url.getPath();
		InputStream in = new FileInputStream(new File(path));
		
		// 1) 使用properties对象
		Properties prop = new Properties();
		// 2) 使用load方法加载properties文件
		prop.load(in);
		// 3) 通过getProperty() 获取内容
		System.out.println(prop.getProperty("name"));
		System.out.println(prop.getProperty("password"));
		
		
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
	}
	
	
	
}

原文地址:https://www.cnblogs.com/mzywucai/p/11053517.html