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

Servlet Init Parameter(์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ web.xml์— DB ์—ฐ๊ฒฐ ์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ

by Leica 2020. 2. 11.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ ๊ธ€

 - Context Parameter(์ปจํ…์ŠคํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ web.xml์— DB ์—ฐ๊ฒฐ ์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ


Servlet Init Parameter(์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ web.xml์— DB ์—ฐ๊ฒฐ ์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ

Servlet Init Parameter(์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜)

์„œ๋ธ”๋ฆฟ์„ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•  ๋•Œ, ์ฆ‰ init()์„ ํ˜ธ์ถœํ• ๋•Œ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์„œ๋ธ”๋ฆฟ์— ์ „๋‹ฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์ด๋‹ค. ๋ณดํ†ต DB ์—ฐ๊ฒฐ ์ •๋ณด์™€ ๊ฐ™์€ ์ •์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค์Œ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. DDํŒŒ์ผ(web.xml)์˜ ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด์— ์„ค์ •
  2. ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด์„œ ์„œ๋ธ”๋ฆฟ ์†Œ์Šค ์ฝ”๋“œ์— ์„ค์ •

๊ฐ€๋Šฅํ•œ 1๋ฒˆ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์ •ํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค. ์†Œ์Šค ์ฝ”๋“œ์—์„œ ๋ถ„๋ฆฌํ•ด์„œ ์™ธ๋ถ€ ํŒŒ์ผ์— ๋‘๋Š”๊ฒŒ ๋” ๊ด€๋ฆฌํ•˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด DB์—์„œ ํ•™์ƒ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์™€ ์ถœ๋ ฅํ•˜๋Š” StudentListServlet ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค. ์†Œ์Šค ์ฝ”๋“œ ์•ˆ์— DB ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
Properties jdbcProperties = new Properties();
jdbcProperties.setProperty("user""username");
jdbcProperties.setProperty("password""****");
 
try {
    // 1. java.sql.Driver ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด ๋“ฑ๋ก
    // MySQL : com.mysql.cj.jdbc.Driver ํด๋ž˜์Šค
    DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/knou", jdbcProperties);
cs

 

์ด ์„œ๋ธ”๋ฆฟ์ด DB ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ ๋ฐ›์•„์„œ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

web.xml ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด StudentListServlet์˜ ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<servlet>
    <servlet-name>StudentListServlet</servlet-name>
    <servlet-class>com.atoz_develop.spms.servlets.StudentListServlet</servlet-class>
    <init-param>
        <param-name>driver</param-name>
        <param-value>com.mysql.cj.jdbc.Driver</param-value>
    </init-param>
    <init-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3307/knou</param-value>
    </init-param>
    <init-param>
        <param-name>username</param-name>
        <param-value>user</param-value>
    </init-param>
    <init-param>
        <param-name>password</param-name>
        <param-value>****</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>StudentListServlet</servlet-name>
    <url-pattern>/student/list</url-pattern>
</servlet-mapping>
cs

<servlet> ํ•˜์œ„์˜ <init-param>์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค.

<param-name>์— ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์„, <param-value>์— ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

์œ„์™€ ๊ฐ™์ด <init-param>์„ ๋„ค๊ฐœ ์ž‘์„ฑํ•ด์„œ JDBC ๋“œ๋ผ์ด๋ฒ„ ํด๋ž˜์Šค, JDBC URL, ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์„ค์ •ํ•œ๋‹ค.

user์™€ ****์—๋Š” ์‹ค์ œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  StudentListServlet์˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*Properties jdbcProperties = new Properties();
jdbcProperties.setProperty("user", "username");
jdbcProperties.setProperty("password", "****");*/
 
try {
    // 1. java.sql.Driver ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด ๋“ฑ๋ก
    // MySQL : com.mysql.cj.jdbc.Driver ํด๋ž˜์Šค
    // DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
    // conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/knou", jdbcProperties);
    // ์„œ๋ธ”๋ฆฟ init parameter๋ฅผ ์ด์šฉํ•œ JDBC ์—ฐ๊ฒฐ
    Class.forName(this.getInitParameter("driver"));
    conn = DriverManager.getConnection(
            this.getInitParameter("url"),
            this.getInitParameter("username"),
            this.getInitParameter("password")
    );
 
    stmt = conn.createStatement();
cs

 

Servlet init parameter๋Š” getInitParameter()๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

getInitParameter()๋Š” ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์˜ ๋ฐฐ์น˜ ์ •๋ณด๊ฐ€ ์žˆ๋Š” web.xml๋กœ๋ถ€ํ„ฐ <init-param>์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ String์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ด์ „ ๋ฐฉ์‹์ธ DriverManager.registerDriver()์—๋Š” JDBC ๋“œ๋ผ์ด๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•˜์ง€๋งŒ Class.forName()์—๋Š” ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•œ ํด๋ž˜์Šค ์ด๋ฆ„(QName)์„ ์ „๋‹ฌํ•ด์„œ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•œ๋‹ค.

 

์ˆ˜์ •๋œ ์œ„ ์ฝ”๋“œ๋Š” getInitParameter()๋กœ JDBC ๋“œ๋ผ์ด๋ฒ„ ํด๋ž˜์Šค, JDBC URL, ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์„œ DB์— ์—ฐ๊ฒฐํ•˜์˜€๋‹ค.

 

์ด์ฒ˜๋Ÿผ ์ž๋ฐ” ์†Œ์Šค ์ฝ”๋“œ ์•ˆ์— ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ์œ ์ง€๋ณด์ˆ˜ํ•˜๋Š”๋ฐ ํ›จ์”ฌ ๋‚ซ๋‹ค.

๊ทธ๋ฆฌ๊ณ  servlet init parameter๋Š” ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ DB ์—ฐ๊ฒฐ ์ •๋ณด์™€ ๊ฐ™์€ ์—ฌ๋Ÿฌ ์„œ๋ธ”๋ฆฟ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ’์€ context param(์ปจํ…์ŠคํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜)๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค.

 

[์ฐธ๊ณ ] Annotation์— Servlet Init Parameter ์„ค์ •ํ•˜๊ธฐ

๋‹ค์Œ๊ณผ ๊ฐ™์ด web.xml์ด ์•„๋‹Œ ์„œ๋ธ”๋ฆฟ ์†Œ์Šค ์ฝ”๋“œ์— ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ servlet init parameter๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
@WebServlet(
        urlPatterns = {"/student/update"},
        initParams = {
                @WebInitParam(name="driver", value="com.mysql.cj.jdbc.Driver"),
                @WebInitParam(name="url", value="jdbc:mysql://localhost:3307/knou"),
                @WebInitParam(name="username", value="user"),
                @WebInitParam(name="password", value="****")
        }
)
cs

 

@WebServlet ์–ด๋…ธํ…Œ์ด์…˜์˜ initParams๋Š” @WebInitParam์˜ ๋ฐฐ์—ด๋กœ servlet init parameter๋ฅผ ์„ค์ •ํ•˜๋Š” ์†์„ฑ์ด๋‹ค.

@WebInitParam์€ name(๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„), value(๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’)์˜ ํ•„์ˆ˜ ์†์„ฑ์„ ๊ฐ–๊ณ  ์„ ํƒ ์†์„ฑ์ธ description(์„ค๋ช…)์ด ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ๋‹ค.


  • ๊ด€๋ จ ๊ธ€

 - Context Parameter(์ปจํ…์ŠคํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ web.xml์— DB ์—ฐ๊ฒฐ ์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€