J2EE之ServletContext读取资源文件
ServletContext读取资源文件内容的方式有两种:
方法1.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/data.properties");
Properties pros = new Properties();
pros.load(in); String username = pros.getProperty("username");
String password = pros.getProperty("password"); System.out.println("username = " + username);
System.out.println("password = " + password);
}
这里须要注意的是data.properties文件的位置在Myeclipse的src文件夹下,为啥getResourceAsStream方法传入的參数确实"/WEB-INF/classes/data.properties"
这是由于这些代码有webserver运行,当项目公布以后。data.properties文件就会被放到tomcat安装文件所在目录下。
如图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTYzMTUyMTM4Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
所以这里传入參数就解释清楚了。
方法2
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String path = this.getServletContext().getRealPath("/WEB-INF/classes/data.properties");
FileInputStream in = new FileInputStream(path);
Properties pros = new Properties();
pros.load(in); String username = pros.getProperty("username");
String password = pros.getProperty("password"); System.out.println("username = " + username);
System.out.println("password = " + password);
}
这里首先通过getRealPath方法获取data.properties文件的绝对路径,然后通过FileInputStream获取文件流。
J2EE之ServletContext读取资源文件的更多相关文章
- Java/JavaWeb中读取资源文件
1.一般工程中使用I/O类指定文件的绝对路径读取 FileInputStream fis = new FileInputStream("src/main/resources/zsm.prop ...
- 使用ServletContext对象读取资源文件
备注:本文以properties文件为例 一.通过ServletContext读取文件 1.通过ServletContext读取放置在src下的properties文件 package com; im ...
- ServletContext类 (共享数据+获取初始化的参数+请求转发+读取资源文件)
ServletContext对象 web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的 web应用: 作用 1.共享数据 (一般用sessio ...
- WEB应用中的普通Java程序如何读取资源文件
package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Serv ...
- 在javaweb中通过servlet类和普通类读取资源文件
javaweb有两种方式读取资源文件 在Servlet中读取,可以使用servletContext,servletContext可以拿到web所有的资源文件,然后随便读,但是这种方法不常用,尽量少在S ...
- Java-Servlet--《12-WEB应用中的普通Java程序如何读取资源文件.mp4》 有疑问
\第五天-servlet开发和ServletConfig与ServletContext对象\12-WEB应用中的普通Java程序如何读取资源文件.mp4; 多层时,DAO为了得到资源文件中的配置参数: ...
- JavaWeb读取资源文件的四种方式
1. ServletContext 1. 调用getResourcesAsStream方法获取输入流, 相对于webroot, 不用加/2. 读取任何类型的文件3. *只能在web环境下使用 Inpu ...
- maven下读取资源文件的问题(转)
原文链接:http://shenchao.me/2016/04/20/maven%E4%B8%8B%E8%AF%BB%E5%8F%96%E8%B5%84%E6%BA%90%E6%96%87%E4%BB ...
- 【解惑】深入jar包:从jar包中读取资源文件
[解惑]深入jar包:从jar包中读取资源文件 http://hxraid.iteye.com/blog/483115 TransferData组件的spring配置文件路径:/D:/develop/ ...
随机推荐
- POJ 2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
- c++ const char *c_str(); 坑的学习
语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过st ...
- QML嵌入到QWidget中方法
简介 嵌入方法有两种一种是直接拖控件,另一种是cpp代码动态生成, 控件方式 动态代码生成 QQuickWidget *m_quickWidget=new QQuickWidget(); QUrl s ...
- ContentLoadingProgressBar不显示问题
ContentLoadingProgressBar需要设置style 并且在XML中布局的位置必须写在content布局的下面 <?xml version="1.0" enc ...
- Form - 遍历行
go_block('block_name'); first_record; LOOP message(:block_name.item); if :system.last_record = ...
- MVC——数据库增删改查(Razor)——Html语法
一.显示界面 .Models(模板) private MyDBDataContext _context = new MyDBDataContext(); public List<Info> ...
- 如何使用eclipse进行嵌入式Linux的开发
如何使用eclipse进行嵌入式Linux的开发 作者:曾宏安,华清远见嵌入式学院高级讲师. 如何使用eclipse进行嵌入式Linux的开发 习惯了在windows环境下开发的程序员在转到Linux ...
- POJ -- 3842
An Industrial Spy Description Industrial spying is very common for modern research labs. I am such ...
- lua package.path的使用
需要用lua写一个工具,c++调用lua,然后这个lua要require其他lua脚本,在主lua里面设置package.path,但一直都失败,甚至lua都无法编译通过. project_path ...
- Vim 的粘贴复制功能
Vim作为最好用的文本编辑器之一,使用vim来编文档,写代码实在是很惬意的事情.每当学会了vim的一个新功能,就会很大地提高工作效率.有人使用vim几十年,还没有完全掌握vim的功能,这也说明了vim ...