web项目中servlet利用servletContext对象读取资源文件

package test;

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;

/**
 * servlet中庸servletContext读取资源文件
 */
public class ServletDemo11 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		// 资源文件在src下,那么在web服务器中的位置是WEB-INF/classes下
		// /代表web程序的根目录
		InputStream in = this.getServletContext().getResourceAsStream(
				"/WEB-INF/classes/db.properties");
		//资源在包中,那么在web服务器中的位置是WEB-INF/classes/package下
		InputStream in = this.getServletContext().getResourceAsStream(
				"/WEB-INF/classes/test/db.properties");
		//资源在WebRoot下,那么在web服务器中的位置是web程序的根目录下
		InputStream in = this.getServletContext().getResourceAsStream(
						"/db.properties");
		read(in);
		*/
		//chuanTong();
		chuanTong2();
	}
	
	/**
	 * 传统的SE读取资源文件方式是错误的:
	 */
	public void chuanTong() {
		//在src下
		try {
			//这样是读取不出来的,因为相对路径是相对于虚拟机加载的路径,也就是tomcat启动时的bin目录
			//所以在web程序中读取资源时要用servletContext对象
			FileInputStream fin = new FileInputStream("WEB-INF/classes/db.properties");
			read(fin);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 传统的SE读取资源文件方式是错误的:
	 * 但是如果先用servletContext对象拿到资源的绝对路径是可以的
	 * 而且这种方法还有个好处就是可以得到文件的名称,getResourceAsStream是不能获取资源名称的
	 * 比如做下载时,就需要拿到资源名称
	 */
	public void chuanTong2() {
		//在src下
		try {
			String path = this.getServletContext().getRealPath("WEB-INF/classes/db.properties");
			String name = path.substring(path.lastIndexOf("\\")+1);
			System.out.println("文件名称" + name);
			FileInputStream fin = new FileInputStream(path);
			read(fin);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 
	 * @param in
	 * @throws IOException
	 * 抽取出来的代码
	 */
	private void read(InputStream in) throws IOException {
		Properties prop = new Properties();
		prop.load(in);
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ServletContext接口可以方便地读取Web应用程序源,例如配置文件、图片、样式表等。 以下是一个示例程序,演示如何使用ServletContext接口读取Web应用程序源: ```java import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResourceServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取ServletContext对象 ServletContext context = getServletContext(); // 读取源文件 InputStream input = context.getResourceAsStream("/WEB-INF/config.properties"); // 输出到客户端 byte[] buffer = new byte[1024]; int length; while ((length = input.read(buffer)) != -1) { response.getOutputStream().write(buffer, 0, length); } // 关闭输入流 input.close(); } } ``` 在上述示例,`getResourceAsStream`方法接收一个源路径参数,可以是相对路径或绝对路径。如果路径以斜杠开头,表示相对于Web应用程序的根目录;如果路径不以斜杠开头,表示相对于Servlet类所在的目录。 在本例源文件`config.properties`位于`WEB-INF`目录下,因此路径以斜杠开头。程序先获取ServletContext对象,然后使用该对象的`getResourceAsStream`方法读取源文件,并将其输出到客户端。 需要注意的是,在使用ServletContext读取源时,需要保证源文件存在并可读,否则会抛出异常。此外,如果读取的是文本文件,还需要考虑编码问题,避免出现乱码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值