黑马程序员--ServletContext之三种方式读取配置文件
2014-03-23 10:20
465 查看
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
配置文件分两种,如果是数据之间有关系,就使用Xml,如果数据之间没有关系,比如数据库的配置文件,则使用properties配置文件WEB中ServletContext三种方式读取properties配置文件
1.如果你需要读取的文件要知道文件名整个路径,请使用getRealPath方法
2.读取properties配置文件时
Properties prop=new Properties();
prop.load(in);
这是模板代码
3.test3()这方法已经不经常使用,但是举例了出来
db.properties文件中的内容是:
username=root
password=root
url=jdbc:mysql://localhost:3306/test
Servlet文件内容:
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>
配置文件分两种,如果是数据之间有关系,就使用Xml,如果数据之间没有关系,比如数据库的配置文件,则使用properties配置文件WEB中ServletContext三种方式读取properties配置文件
1.如果你需要读取的文件要知道文件名整个路径,请使用getRealPath方法
2.读取properties配置文件时
Properties prop=new Properties();
prop.load(in);
这是模板代码
3.test3()这方法已经不经常使用,但是举例了出来
db.properties文件中的内容是:
username=root
password=root
url=jdbc:mysql://localhost:3306/test
Servlet文件内容:
package cn.itcast.servlet; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; 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 ServletDemo11 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //test1(); //test2(); test3(); } private void test1() throws IOException { ServletContext sc=getServletContext(); InputStream in=sc.getResourceAsStream("/db.properties"); //这里返回流 Properties prop=new Properties(); prop.load(in); String username=prop.getProperty("username"); String password=prop.getProperty("password"); String url=prop.getProperty("url"); System.out.println(username); System.out.println(password); System.out.println(url); } private void test2() throws IOException { ServletContext sc=getServletContext(); String path=sc.getRealPath("/db.properties");//返回的值是C:\...这种的原文件路径 //例如D:\Program Files (x86)\MyEclipse 6.5\work\flx05\WebRoot\db.properties FileInputStream in=new FileInputStream(path); Properties prop=new Properties(); prop.load(in); String username=prop.getProperty("username"); String password=prop.getProperty("password"); String url=prop.getProperty("url"); System.out.println(username); System.out.println(password); System.out.println(url); } private void test3() throws IOException { ServletContext sc=getServletContext(); URL url=sc.getResource("/db.properties"); InputStream in=url.openStream(); Properties prop=new Properties(); prop.load(in); String username=prop.getProperty("username"); String password=prop.getProperty("password"); String neturl=prop.getProperty("url"); System.out.println(username); System.out.println(password); System.out.println(neturl); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>
相关文章推荐
- springboot读取配置文件的三种方式
- java 三种读取配置文件的方式
- Spring读取配置文件的三种方式
- springboot读取配置文件的三种方式
- 用ServletContext读取资源文件的三种方式
- 读取资源(配置)文件的三种方式
- java 学习笔记 读取配置文件的三种方式
- NHibernate读取配置文件的三种方式
- java 学习笔记 读取配置文件的三种方式
- ServletContext中getRealPath()读取文件及其他三种读取文件的方式及其区别
- Java读取配置文件的三种方式
- 三种方式实现Flex读取XML配置文件
- java中读取properties配置文件中的内容的三种方式
- 通过servletcontext读取WEB-INF下的配置文件
- java 4种方式读取配置文件 + 修改配置文件
- php读取文件内容的三种方式
- 使用FileReader类、BufferedReader类、FileInputStream类三种方式进行文件的读取
- 读取风云二号文件三种方式(主要用于学习IDL读取图像)
- 黑马程序员_用类加载器的方式管理资源和配置文件简单介绍
- spring中读取xml配置文件、获取bean的几种方式