Java·ο»ΏServlet·ο»ΏJSP

JAVA μ›Ή ν”„λ‘œμ νŠΈμ—μ„œ Properties 파일 ν™œμš©ν•˜κΈ°

Leica 2020. 2. 20. 05:48
λ°˜μ‘ν˜•

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에 μ„€μ •ν•œ νŒŒλΌλ―Έν„° 이름을 λ„˜κ²¨ ν”„λ‘œνΌν‹° 파일의 경둜 정보λ₯Ό κ°€μ Έμ˜¨λ‹€.

 

μ‹€ν–‰ κ²°κ³ΌλŠ” 이전과 λ™μΌν•˜λ‹€.

λ°˜μ‘ν˜•