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

ServletContextListener๋กœ DB ์ปค๋„ฅ์…˜, DAO ๊ณต์œ  ๊ฐ์ฒด ๊ด€๋ฆฌํ•˜๊ธฐ

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

ServletContextListener๋กœ DB ์ปค๋„ฅ์…˜, DAO ๊ณต์œ  ๊ฐ์ฒด ๊ด€๋ฆฌํ•˜๊ธฐ

ํ•„์š”ํ• ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋งŽ์€ garbage๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์‹คํ–‰ ์‹œ๊ฐ„์ด ๊ธธ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๋Ÿฌ ์„œ๋ธ”๋ฆฟ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘ํ•  ๋•Œ ์ค€๋น„ํ•ด๋‘ฌ์„œ ์„œ๋กœ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์ด ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋‚˜ ์‹คํ–‰ ์†๋„ ์ธก๋ฉด์—์„œ ์ข‹๋‹ค.

 

๐Ÿšจ ์‹ค์ œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹ฑ๊ธ€ DB ์ปค๋„ฅ์…˜์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•ˆ๋œ๋‹ค!
๋ณธ ํฌ์ŠคํŒ…์˜ ์ค‘์‹ฌ ๋‚ด์šฉ์€ ServletContextListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋  ๋•Œ ํ•„์š”ํ•œ ์ค€๋น„์ž‘์—…์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

Listener

ํ†ฐ์บฃ๊ฐ™์€ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๋„๋ก ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘๋ถ€ํ„ฐ ์ข…๋ฃŒ๊นŒ์ง€ ์ฃผ์š”ํ•œ ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ์•Œ๋ฆผ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์ด๋ฒคํŠธ์— ๋”ฐ๋ผ ์ •ํ•ด์ง„ Listener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์„œ DDํŒŒ์ผ(web.xml)์— ๋“ฑ๋กํ•˜๋ฉด ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์›ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ถ„๋ฅ˜ ์ด๋ฒคํŠธ Listener ์ธํ„ฐํŽ˜์ด์Šค
์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘, ์ข…๋ฃŒ javax.servlet.ServletContextListener
ServletContext์— ๊ฐ’ ์ถ”๊ฐ€, ์ œ๊ฑฐ, ๋Œ€์ฒด javax.servlet.ServletContextAttributeListener
์„ธ์…˜ ์ƒ์„ฑ, ์†Œ๋ฉธ javax.servlet.http.HttpSessionListener
ํ™œ์„ฑ, ๋น„ํ™œ์„ฑ javax.servlet.http.HttpSessionActivationListener
HttpSession์— ๊ฐ’ ์ถ”๊ฐ€, ์ œ๊ฑฐ, ๋Œ€์ฒด javax.servlet.http.HttpSessionAttributeListener
์š”์ฒญ ์š”์ฒญ, ์‘๋‹ต javax.servlet.ServletRequestListener
ServletRequest์— ๊ฐ’ ์ถ”๊ฐ€, ์ œ๊ฑฐ, ๋Œ€์ฒด javax.servlet.ServletRequestAttributeListener

ํ‘œ - ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ฃผ์š” ์ด๋ฒคํŠธ์™€ Listener ์ธํ„ฐํŽ˜์ด์Šค

 

ServletContextListener์˜ ํ™œ์šฉ

์ด ์ค‘ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘, ์ข…๋ฃŒ ์‹œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ํ™œ์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ค€๋น„ํ•œ ๋’ค ServletContext์— ์ €์žฅํ•ด์„œ ๊ณต์œ  ๊ฐ์ฒด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด์ „์— ์„œ๋ธ”๋ฆฟ์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ <load-on-startup> ํƒœ๊ทธ ์„ค์ •์œผ๋กœ DB ์ปค๋„ฅ์…˜ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์—ˆ๋Š”๋ฐ ๊ทธ ๋ณด๋‹ค ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์ด๋ ‡๊ฒŒ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ด์šฉํ•˜๋Š”๊ฒƒ์ด๋‹ค.

 

์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜๋ฉด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” ServletContextListener์˜ ๊ตฌํ˜„์ฒด์— ๋Œ€ํ•ด contextInitialized()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋˜๋ฉด contextDestroyed()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ ServletContextListener๋ฅผ ํ™œ์šฉํ•ด์„œ DB ์ปค๋„ฅ์…˜๊ณผ DAO์˜ ๊ณต์œ  ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๋ ค๋ฉด SerlvetContextListener์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  contextInitialized() ๋ฉ”์†Œ๋“œ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ, contextDestroyed()์— ์ž์›์„ ํ•ด์ œํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

 

1. ServletContextListener ๊ตฌํ˜„

listeners ํŒจํ‚ค์ง€์— ContextLoaderListener ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

โ€ป ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ๋„ ๊ฐ™์€ ์—ญํ• ์˜ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

ContextLoaderListener.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class ContextLoaderListener implements ServletContextListener {
    Connection conn;
 
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext sc = sce.getServletContext();
 
        try {
            // Connection ์ค€๋น„
            Class.forName(sc.getInitParameter("driver"));
            conn = DriverManager.getConnection(
                    sc.getInitParameter("url"),
                    sc.getInitParameter("username"),
                    sc.getInitParameter("password")
            );
 
            // Dao ์ค€๋น„
            StudentDao studentDao = new StudentDao();
            studentDao.setConnection(conn);
 
            // Dao ์ €์žฅ
            sc.setAttribute("studentDao", studentDao);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // Connection ํ•ด์ œ
        try {
            conn.close();
        } catch (SQLException e) { }
    }
}
cs

 

2. ๋ฆฌ์Šค๋„ˆ ๋ฐฐ์น˜

๋ฆฌ์Šค๋„ˆ ๋ฐฐ์น˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ DDํŒŒ์ผ(web.xml)์— ๋ฆฌ์Šค๋„ˆ ์„ ์–ธ๋ถ€๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

1) ์–ด๋…ธํ…Œ์ด์…˜

ํด๋ž˜์Šค ์„ ์–ธ ์œ„์— @WebListener ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ๋‹ค.

 

1
2
3
4
5
6
7
@WebListener
public class ContextLoaderListener implements ServletContextListener {
    Connection conn;
 
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext sc = sce.getServletContext();
cs

 

2) web.xml

web.xml์˜ <web-app> ํƒœ๊ทธ ์•ˆ์— ์ž‘์„ฑํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ Servlet 2.4 ์ดํ•˜์—์„œ๋Š” <filter-mapping> → <listener> → <serlvet> ์ˆœ์„œ๋กœ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค.

Servlet 2.5๋ถ€ํ„ฐ๋Š” ์ˆœ์„œ์— ์ƒ๊ด€ ์—†๋‹ค.

 

web.xml

1
2
3
4
<!-- ๋ฆฌ์Šค๋„ˆ ์„ ์–ธ -->
<listener>
    <listener-class>com.atoz_develop.spms.listeners.ContextLoaderListener</listener-class>
</listener>
cs

 

3. ๊ธฐ์กด ์„œ๋ธ”๋ฆฟ ๋ณ€๊ฒฝ

DAO ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๋Œ€์‹  ServletContext์— ์ €์žฅ๋œ DAO ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด ์“ฐ๋„๋ก ๋ณ€๊ฒฝํ•œ๋‹ค.

 

๊ธฐ์กด ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@WebServlet("/student/list")
public class StudentListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ServletContext sc = this.getServletContext();
            Connection conn = (Connection) sc.getAttribute("conn");
 
            StudentDao studentDao = new StudentDao();
            studentDao.setConnection(conn);
 
            req.setAttribute("students", studentDao.selectList());
 
            resp.setContentType("text/html; charset=UTF-8");
            RequestDispatcher rd = req.getRequestDispatcher(
                    "/student/StudentList.jsp"
            );
            rd.include(req, resp);
        } catch (SQLException e) {
            req.setAttribute("error", e);
            RequestDispatcher rd = req.getRequestDispatcher("/Error.jsp");
            rd.forward(req, resp);
        }
    }
}
cs

 

๋ณ€๊ฒฝ ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@WebServlet("/student/list")
public class StudentListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ServletContext sc = this.getServletContext();
            req.setAttribute("students", ((StudentDao) sc.getAttribute("studentDao")).selectList());
 
            resp.setContentType("text/html; charset=UTF-8");
            RequestDispatcher rd = req.getRequestDispatcher(
                    "/student/StudentList.jsp"
            );
            rd.include(req, resp);
        } catch (SQLException e) {
            req.setAttribute("error", e);
            RequestDispatcher rd = req.getRequestDispatcher("/Error.jsp");
            rd.forward(req, resp);
        }
    }
}
cs

 

์ œ๋Œ€๋กœ ๊ตฌํ˜„ ํ–ˆ๋‹ค๋ฉด ์ฝ”๋“œ ๋ณ€๊ฒฝ ํ›„์—๋„ ๋™์ผํ•˜๊ฒŒ ์ •์ƒ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ServletContext์— ์ €์žฅ๋ผ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰ ์†๋„๊ฐ€ ๋นจ๋ผ์ง€๊ณ  garbage๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€