package com.mahe666.servlet;

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.io.PrintWriter;
import java.util.Properties;

public class PropertiesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //  get的路径是编译好的target文件夹下的路径
        //  / 代表当前web项目（servlet-02-1.0-SNAPSHOT）
        //  一定要看准编译后的资源文件的路径
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        //  返回的流咱们就可以用了
        Properties prop = new Properties();
        prop.load(is);
        String username = prop.getProperty("username");
        String passowrd = prop.getProperty("password");

        PrintWriter writer = resp.getWriter();
        writer.println(username);
        writer.println(passowrd);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
