A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

利用ServletContext对象读取资源文件在项目src文件夹下创建db1.properties配置文件,内容如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123


然后将该文件复制两份,一份放在web文件夹下,取名为db2.properties。另一份放在cn.itcast包下,取名为db3.properties



新建一个PathServlet.java文件,内容如下
[Java] 纯文本查看 复制代码
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;


public class PathServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决浏览器输出乱码问题
        response.setHeader("content-type","text/html;charset=UTF-8");
        String s1="/WEB-INF/classes/db1.properties";
        //通过ServletContext对象读取src目录下的properties配置文件
        readPropCfgFile(response,s1);
        response.getWriter().println("<hr/>");
       //通过ServletContext对象读取Web目录下的properties配置文件,“/”代表的是项目根目录
        String s2 = "/db2.properties";
        readPropCfgFile(response,s2);
        response.getWriter().println("<hr/>");
        //读取src目录下的cn.itcast包中的db3.properties配置文件
        String s3 = "/WEB-INF/classes/cn/itcast/db3.properties";
        readPropCfgFile(response,s3);

    }

    /**
     * 根据指定位置读取相应的properties配置文件
     * @param response
     */
    private void readPropCfgFile(HttpServletResponse response,String s) throws IOException {
        //根据路径获取文件的输入流
        InputStream in = this.getServletContext().getResourceAsStream(s);

                Properties prop = new Properties();
                prop.load(in);
                String driver = prop.getProperty("driver");
               String url = prop.getProperty("url");
               String username = prop.getProperty("username");
               String password = prop.getProperty("password");
               response.getWriter().println("读取"+s+"------:");
               response.getWriter().println(
                       MessageFormat.format(
                               "driver={0},url={1},username={2},password={3}",
                                            driver,url, username, password));

    }
}


web.xml文件配置加上如下内容


[XML] 纯文本查看 复制代码
<servlet>
        <servlet-name>PathServlet</servlet-name>
        <servlet-class>cn.itcast.PathServlet</servlet-class>
    </servlet>
   <servlet-mapping>
       <servlet-name>PathServlet</servlet-name>
       <url-pattern>/path</url-pattern>
   </servlet-mapping>



使用类装载器读取资源文件
把改造一下PathServlet即可
[Java] 纯文本查看 复制代码
package cn.itcast;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;


public class PathServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决浏览器输出乱码问题
        response.setHeader("content-type","text/html;charset=UTF-8");
        response.getWriter().println("根据路径读取配置文件:<hr/>");
        String s1="/WEB-INF/classes/db1.properties";
        //通过ServletContext对象读取src目录下的properties配置文件
        readPropCfgFile(response,s1);
        response.getWriter().println("<hr/>");
       //通过ServletContext对象读取Web目录下的properties配置文件,“/”代表的是项目根目录
        String s2 = "/db2.properties";
        readPropCfgFile(response,s2);
        response.getWriter().println("<hr/>");
        //读取src目录下的cn.itcast包中的db3.properties配置文件
        String s3 = "/WEB-INF/classes/cn/itcast/db3.properties";
        readPropCfgFile(response,s3);
        response.getWriter().println("<hr/>");
        response.getWriter().println("类加载器读取配置文件:<hr/>");

        //读取src目录下的properties配置文件
        String s4 = "db1.properties";
        readClassLoadCfgFile(response,s4);
        response.getWriter().println("<hr/>");
        //读取src目录下的cn.itcast包中的db3.properties配置文件
        String s5="/cn/itcast/db3.properties";
        readClassLoadCfgFile(response,s5);


    }

    /**
     * 根据指定路径读取相应的properties配置文件
     * @param response
     */
    private void readPropCfgFile(HttpServletResponse response,String s) throws IOException {
        //根据路径获取文件的输入流
        InputStream in = this.getServletContext().getResourceAsStream(s);

                Properties prop = new Properties();
                prop.load(in);
                String driver = prop.getProperty("driver");
               String url = prop.getProperty("url");
               String username = prop.getProperty("username");
               String password = prop.getProperty("password");
               response.getWriter().println("根据指定路径读取"+s+"------:");
               response.getWriter().println(
                       MessageFormat.format(
                               "driver={0},url={1},username={2},password={3}",
                                            driver,url, username, password));

    }

    /**
     * 根据类装载器读取相应properties配置文件
     */
    private void readClassLoadCfgFile(HttpServletResponse response,String s) throws IOException {

        //获取到装载当前类的类装载器
        ClassLoader loader = PathServlet.class.getClassLoader();

        InputStream in = loader.getResourceAsStream(s);

        Properties prop = new Properties();
        prop.load(in);
        String driver = prop.getProperty("driver");
        String url = prop.getProperty("url");
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");
        response.getWriter().println("用类装载器读取"+s+"------");
        response.getWriter().println(
                MessageFormat.format(
                        "driver={0},url={1},username={2},password={3}",
                             driver,url, username, password));
    }
}








1 个回复

倒序浏览

很不错,受教了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马