package donghongyujava.test_jdbc;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
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 Test_jdbc extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取ServletContest对象
ServletContext sct = this.getServletConfig().getServletContext();
// 获取需要解析的文件所在服务器的绝对路径
String realPath = sct.getRealPath("/WEB-INF/classes/jdbc.properties");
System.out.println(realPath);
// 根据绝对路径创建File的对象那个
File jdbc = new File(realPath);
// 打印流对象
displayProps(new FileInputStream(jdbc));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void displayProps(InputStream inStream) throws IOException {
// 创建properties操作对象
Properties prop = new Properties();
// 把流对象载入
prop.load(inStream);
// 获取所有的键名称
Enumeration<?> enums = prop.propertyNames();
// 遍历结果集
while (enums.hasMoreElements()) {
//获取集合的每个元素,即获取键名称
String key = (String) enums.nextElement();
//根据键获取所对应的值
String value = prop.getProperty(key);
//输出
System.out.println(key + "==" + value);
}
}
}
jdbc.properties文件内容
url=jdbc\:mysql\://localhost\:3306/java_web
user=admin
pass=root
效果图
分享到:
相关推荐
ServletContext读取web应用中的资源文件
quartz servletcontext quartz servletcontext配置资料quartz servletcontext配置资料
超全面javaweb教程28天第9天_14_获取ServletContext对象
这是servlet中ServletContext的应用实例,主要就是添加属性和读取属性。
超全面javaweb教程28天第9天_17_ServletContext获取资源相关方法
超全面javaweb教程28天第9天_16_ServletContext获取公共的初始化参数
ServletContext代码,开发工具 Myeclipse
ServletContext与ServletConfig关系
博客地址:...存放数据指的是这三个对象在工程中setAttribute(),removeAttribute()的时候,我们的监听器就会知道,然后在对应的方法中执行相应的操作。
Ssh获取ServletContext 只需要更改struts-config action中的type就可以了ru :type="com.uo.spring.SpringDelegatingActionProxy" 大家有什么疑问可以直接去我的blog查看http://blog.csdn.net/chen1255/ 《[正解]Ssh...
对servletContext的学习,并使用实例进行了实战。完成了一个登录和统计登录人数的功能。
主要介绍了Spring实战之ServletContextResource访问资源文件,结合实例形式分析了spring使用ServletContextResource读取与遍历资源文件相关操作技巧,需要的朋友可以参考下
NULL 博文链接:https://onway417.iteye.com/blog/2194540
(5)ServletContext对象可以获取一些全局的配置信息,例如数据库的一些信息,可以配置xml文件当中 ,之后,通过ServletContext对象获取。这样配置的好处是每个servlet都可以直接访问,而不需要为每 个servlet进行...
ssm整合步骤: 第一步:mybatis单独测试 1.引入mybatis,mysql依赖 2....注意:dao层只有接口。... 整合的要点:就是让springmvc从servletContext中获取spring容器即可 1.在web.xml中配置spring的监听器即可。
注意:每个servlet有一个ServletConfig,每个Web应用有一个ServletContext.要把初始化参数认为是部署时常量,可用在运行时得到这些初始化参数,但不能设置初始化参数,根本没有setInitParameter().如果修改XML来改变...
介绍 ServletContext对象的一些重要方法的示例代码
当服务器停掉后,重新启动。这个网页计数器又要重新开始计数。这显然不符合逻辑。故需要想一个办法,即使在服务器被停掉后,网页计数器也能正常计数。(使用了io流技术)
通过这两个接口,我们可以在web.xml中设置一些参数,如数据库地址、用户名密码等,供 Servlet 使用, 这样每次参数值变动时,不用重新编译Servlet了。 javax.servlet.ServletContext接口(上下文参数)是针对整个项目...
主要介绍了JavaWeb读取配置文件的4种方法,方法一采用ServletContext读取,方法二采用ResourceBundle类读取配置信息,方法三采用ClassLoader方式进行读取配置信息,对javaweb读取配置文件的四种方法感兴趣的朋友参考...