๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

JAVA ์›น ํ”„๋กœ์ ํŠธ์—์„œ Properties ํŒŒ์ผ ํ™œ์šฉํ•˜๊ธฐ

by Leica 2020. 2. 20.
๋ฐ˜์‘ํ˜•

JAVA ์›น ํ”„๋กœ์ ํŠธ์—์„œ Properties ํŒŒ์ผ ํ™œ์šฉํ•˜๊ธฐ 

์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘ํ•˜์ž๋งˆ์ž ๊ฐ€์ ธ์™€์•ผํ•˜๋Š” ์ •๋ณด์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์— ์ž‘์„ฑํ•ด์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์›น ํ”„๋กœ์ ํŠธ์—์„œ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด์„œ ๊บผ๋‚ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

 

ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ ์ž‘์„ฑํ•˜๊ธฐ

ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์€ ํ•œ ๋ผ์ธ์— key=value ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ  .properties ํ™•์žฅ์ž๋ฅผ ๋ถ™์—ฌ ์ €์žฅํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ ๋กœ๋”ฉ ๋ฐ ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๊ธฐ(์ฝ๊ธฐ)

java.util.Properties ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค. Properties๋Š” key=value ํ˜•์‹์˜ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜์—ฌ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
Properties properties = new Properties();
properties.load(new FileReader("C:\\workspace\\intellij-project\\JavaWebProgramming\\out\\artifacts\\Member_MiniMVCFramework_war_exploded\\WEB-INF\\application-context.properties"));
 
for (Object object: properties.keySet()) {
    System.out.println(object + " = " + properties.get(object));
}
cs

์‹คํ–‰ ๊ฒฐ๊ณผ

/student/list.do = com.atoz_develop.spms.controls.StudentListController
/student/delete.do = com.atoz_develop.spms.controls.StudentDeleteController
studentDao = com.atoz_develop.spms.dao.MySqlStudentDao
/student/update.do = com.atoz_develop.spms.controls.StudentUpdateController
jndi.dataSource = java:comp/env/jdbc/knou
/student/add.do = com.atoz_develop.spms.controls.StudentAddController
/auth/login.do = com.atoz_develop.spms.controls.LogInController
/auth/logout.do = com.atoz_develop.spms.controls.LogOutController

 

Properties์˜ load() ๋ฉ”์†Œ๋“œ๋Š” FileReader๋ฅผ ํ†ตํ•ด ์ฝ์–ด๋“ค์ธ ํ”„๋กœํผํ‹ฐ ๋‚ด์šฉ์„ ํ‚ค-๊ฐ’ ํ˜•ํƒœ๋กœ ๋‚ด๋ถ€ ๋งต์— ๋ณด๊ด€ํ•œ๋‹ค.

 

web.xml์— ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •

ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ณ  web.xml๋กœ๋ถ€ํ„ฐ ์ฝ์–ด์˜ค๊ฒŒ ์ฒ˜๋ฆฌํ•ด๋ณด์ž.

web.xml์— ์ปจํ…์ŠคํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์— ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

web.xml

1
2
3
4
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.properties</param-value>
</context-param>
cs

 

ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•œ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyServlet extends HttpServlet {
 
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext sc = req.getServletContext();
        Properties properties = new Properties();
        properties.load(new FileReader(sc.getRealPath(sc.getInitParameter("contextConfigLocation"))));
 
        for (Object object: properties.keySet()) {
            System.out.println(object + " = " + properties.get(object));
        }
    }
}
cs

 

์ปจํ…์ŠคํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ServletContext ๊ฐ์ฒด์˜ getInitParameter() ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ’์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

web.xml์— ์„ค์ •ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์„ ๋„˜๊ฒจ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์ด์ „๊ณผ ๋™์ผํ•˜๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€