JavaWeb 读资源的问题-2023-2-27

1、db.properties 文件中读取 username 和 password

2、getResourceAsStream ("/web-inf/classes/db.properties")

3、properties.getProperty("username")

public class Demo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(in);
        String user = prop.getProperty("username");
        String pwd = prop.getProperty("password");
        PrintWriter writer = resp.getWriter();
        writer.println(user+":"+pwd);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
<servlet>
    <servlet-name>sd5</servlet-name>
    <servlet-class>com.feijian.Demo05</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>sd5</servlet-name>
    <url-pattern>/sd5</url-pattern>
</servlet-mapping>

 

 

posted @ 2023-02-27 21:48  Rui2022  阅读(10)  评论(0编辑  收藏  举报