黑马程序员--ServletContext之三种方式读取配置文件

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、&lt;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>、&lt;a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

详细请查看:<a href="http://edu.csdn.net" target="blank"> http://edu.csdn.net</a>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值