JavaWeb学习记录(八)——servlet获取配置信息
2015-06-30 16:25
363 查看
jdbc.properties内容如下:
jdbcUrl=jdbc\:mysql\://localhost\:3306/animal
user=root
pass=root
servlet获取资源信息代码如下
public class ResourceServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1();
test2();
}
//方法一
private void test1() throws IOException, FileNotFoundException {
System.out.println("--------------test1--------------");
ServletContext application=getServletContext();
String path=application.getRealPath("/WEB-INF/classes/jdbc.properties");
File file=new File(path);
Properties pro=new Properties();
pro.load(new FileReader(file));
System.out.println(pro.getProperty("jdbcUrl"));
System.out.println(pro.getProperty("user"));
System.out.println(pro.getProperty("pass"));
}
//方法二
private void test2() throws IOException, FileNotFoundException {
System.out.println("--------------test2--------------");
ServletContext application=getServletContext();
URL url=application.getResource("/WEB-INF/classes/jdbc.properties");
Properties pro=new Properties();
pro.load(url.openStream());
System.out.println(pro.getProperty("jdbcUrl"));
System.out.println(pro.getProperty("user"));
System.out.println(pro.getProperty("pass"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
jdbcUrl=jdbc\:mysql\://localhost\:3306/animal
user=root
pass=root
servlet获取资源信息代码如下
public class ResourceServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1();
test2();
}
//方法一
private void test1() throws IOException, FileNotFoundException {
System.out.println("--------------test1--------------");
ServletContext application=getServletContext();
String path=application.getRealPath("/WEB-INF/classes/jdbc.properties");
File file=new File(path);
Properties pro=new Properties();
pro.load(new FileReader(file));
System.out.println(pro.getProperty("jdbcUrl"));
System.out.println(pro.getProperty("user"));
System.out.println(pro.getProperty("pass"));
}
//方法二
private void test2() throws IOException, FileNotFoundException {
System.out.println("--------------test2--------------");
ServletContext application=getServletContext();
URL url=application.getResource("/WEB-INF/classes/jdbc.properties");
Properties pro=new Properties();
pro.load(url.openStream());
System.out.println(pro.getProperty("jdbcUrl"));
System.out.println(pro.getProperty("user"));
System.out.println(pro.getProperty("pass"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
相关文章推荐
- JavaWeb学习记录(七)——MVC操作数据库增删改查与分页功能
- TestNg JAVA 自动化单元测试框架Demo
- TestNg JAVA 自动化单元测试框架Demo
- 2、javaweb listener 对象的属性变更--监听
- 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码
- 浅谈Eclipse的内存分析工具MAT(一)
- 管中窥探java虚拟机(一)
- Eclipse 打不开
- java内存泄漏的定位与分析
- Java多线程编程总结
- MyEclipse创建自己类库
- 【Java Swing探索之路系列】之二:Java Swing布局面板组件
- SpringMVC 的 Controller 返回各种视图的处理方式
- JavaWeb学习记录(三)——网页中文编码问题
- javaServlet
- JavaWeb学习记录(四)——日期和数字的格式转换
- JavaWeb学习记录(二)——防盗链技术
- JavaWeb学习记录(五)——Servlet随机产生验证码
- android开发环境搭建 eclipse ADT
- JavaWeb学习记录(九)——Cookie的增加、删除、查看