web项目中servlet利用servletContext对象读取资源文件
来源:互联网 发布:全球营销网络分布图 编辑:程序博客网 时间:2024/04/30 07:58
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 0
- web项目中servlet利用servletContext对象读取资源文件
- (18)servletContext应用:获取web应用的初始化参数、实现servlet转发、利用servletContext对象读取资源文件
- Servlet之ServletContext读取web应用中的资源文件
- 在servlet中通过servletcontext对象读取资源文件的模板代码
- servletcontext读取web资源文件
- ServletContext对象读取资源文件
- servletContext对象之读取web项目下文件资源
- Servlet之ServletContext获取获取web项目的资源文件
- ServletContext读取web应用中的资源文件
- ServletContext 读取Web应用中的资源文件
- ServletContext读取资源文件
- java_web11-ServletContext读取Web应用中的资源文件
- ServletContext读取Web应用下的文件资源
- 04.ServletContext读取资源文件
- 使用servletContext读取资源文件
- web项目中对于资源文件的读取
- java web项目中对于资源文件的读取
- 项目中读取资源文件
- 统一设计,iOS6也玩扁平化
- 栈
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- 先进先出SQL Server 语句
- 怎样快速的获取积分?
- web项目中servlet利用servletContext对象读取资源文件
- Flex基本标签和常用组件
- Struts2中使用Json+Jquery+Ajax,以及出现的404异常及解决办法,
- Linux 初始 RAM 磁盘(initrd)概述
- SEO接单需谨慎 并非所有的单子都能接
- nodeJS 学习一【Hello World 篇】
- 最新!ENVI 5.1 遥感图像处理软件(含license.dat许可亲测可用)
- spring context 例子
- 存储那些事儿(四):传统商业存储融入云计算之路