您的位置:首页 > 职场人生

黑马程序员--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文件内容:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: