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

[IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (5) ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ(HttpSession)

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

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (5) ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ(HttpSession)


[IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (5) ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ(HttpSession)

๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” HttpSession์„ ํ™œ์šฉํ•ด์„œ ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ์— ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋ คํ•œ๋‹ค.

 

1. ๋กœ๊ทธ์ธ ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ

LogInServlet.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@WebServlet("/auth/login")
public class LogInServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // LogInForm.jsp๋กœ ํฌ์›Œ๋”ฉ
        RequestDispatcher rd = req.getRequestDispatcher("/auth/LogInForm.jsp");
        rd.forward(req, resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
 
        ServletContext sc = req.getServletContext();
 
        try {
            conn = (Connection) sc.getAttribute("conn");
 
            // DB์—์„œ ํ•™์ƒ ์ •๋ณด ์กฐํšŒ
            pstmt = conn.prepareStatement(
                    "SELECT STUDENT_NO, STUDENT_NAME FROM STUDENT" +
                            " WHERE STUDENT_NO = ? AND PASSWORD = ?"
            );
            pstmt.setString(1, req.getParameter("student_no"));
            pstmt.setString(2, req.getParameter("password"));
            rs = pstmt.executeQuery();
 
            // ์ผ์น˜ํ•˜๋Š” ํ•™์ƒ์ด ์žˆ์œผ๋ฉด Student์— ํ•™์ƒ ์ •๋ณด ๋‹ด์Œ
            if(rs.next()) {
                Student student = new Student()
                        .setStudentNo(rs.getString("STUDENT_NO"))
                        .setStudentName(rs.getString("STUDENT_NAME"));
                // HttpSession์— ์ €์žฅ
                HttpSession session = req.getSession();
                session.setAttribute("student", student);
 
                // /student/list๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
                resp.sendRedirect("../student/list");
            } else {
                // ๋กœ๊ทธ์ธ ์‹คํŒจ ์‹œ /auth/LogInFail.jsp๋กœ ํฌ์›Œ๋”ฉ
                RequestDispatcher rd = req.getRequestDispatcher("/auth/LogInFail.jsp");
                rd.forward(req, resp);
            }
        } catch (SQLException e) {
            req.setAttribute("error", e);
            RequestDispatcher rd = req.getRequestDispatcher("/Error.jsp");
            rd.forward(req, resp);
        } finally {
            try {
                if(rs != null) rs.close();
                if(pstmt != null) pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
cs

 

GET ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด doGet()์ด ํ˜ธ์ถœ๋˜์–ด LogInForm.jsp๋กœ ํฌ์›Œ๋”ฉํ•œ๋‹ค.

 

rd.forward(req, resp);

 

์‚ฌ์šฉ์ž๊ฐ€ ํ•™๋ฒˆ, ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•œ ํ›„ POST ์š”์ฒญ์„ ํ•˜๋ฉด doPost()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

doPost()์—์„œ๋Š” DB๋กœ๋ถ€ํ„ฐ ํ•™์ƒ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ  ์ผ์น˜ํ•˜๋Š” ํ•™์ƒ์„ ์ฐพ์œผ๋ฉด Student vo์— ํ•™์ƒ ์ •๋ณด๋ฅผ ๋‹ด๋Š”๋‹ค.

 

Student student = new Student() .setStudentNo(rs.getString("STUDENT_NO")) .setStudentName(rs.getString("STUDENT_NAME"));

 

๊ทธ๋ฆฌ๊ณ  Student ์ธ์Šคํ„ด์Šค๋ฅผ HttpSession์— ๋‹ด๋Š”๋‹ค.

HttpSession์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋Š” ์„ธ์…˜์„ ๋ฌดํšจํ™”์‹œํ‚ฌ๋•Œ ๊นŒ์ง€ ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ์—์„œ ์œ ์ง€๋œ๋‹ค.

HttpSession๊ณผ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ ์ €์žฅ์†Œ์— ๋Œ€ํ•ด์„œ๋Š” ์ด ํฌ์ŠคํŠธ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.

 

HttpSession session = req.getSession();
session.setAttribute("student", student);

 

๋กœ๊ทธ์ธ ์„ฑ๊ณต์ด๋ฉด /student/list (ํ•™์ƒ ๋ชฉ๋ก)์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ํ•œ๋‹ค.

 

resp.sendRedirect("../student/list");

 

๋กœ๊ทธ์ธ์— ์‹คํŒจํ•˜๋ฉด /auth/LogInFail.jsp๋กœ ํฌ์›Œ๋”ฉ ํ•œ๋‹ค.

 

rd.forward(req, resp);

 

2. ๋กœ๊ทธ์ธ ์ž…๋ ฅํผ ์ž‘์„ฑ

์›น ์ฝ˜ํ…์ธ  ํด๋”์— /auth/LogInForm.jsp๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

LogInForm.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>๋กœ๊ทธ์ธ</title>
</head>
<body>
<h2>๋กœ๊ทธ์ธ</h2>
<form action="login" method="post">
    ํ•™๋ฒˆ: <input type="text" name="student_no"><br>
    ๋น„๋ฐ€๋ฒˆํ˜ธ: <input type="password" name="password"><br>
    <input type="submit" value="๋กœ๊ทธ์ธ">
    <input type='button' value='์ทจ์†Œ' onClick='location.href="<%=request.getContextPath()%>/student/list"'>
</form>
</body>
</html>
cs

 

์œ„์™€ ๊ฐ™์ด <form> ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•ด์„œ LogInServlet์— POST๋กœ ์š”์ฒญ์„ ์ „์†กํ•œ๋‹ค.

'์ทจ์†Œ' ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์‹œ ํ•™์ƒ ๋ชฉ๋ก์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

 

3. ๋กœ๊ทธ์ธ ์‹คํŒจ ์‹œ ์ถœ๋ ฅํ•  JSP ์ž‘์„ฑ

์›น ์ฝ˜ํ…์ธ  ํด๋”์— /auth/LogInFail.jsp๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

LogInFail.jsp

1
2
3
4
5
6
7
8
9
10
11
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Refresh" content="3; url=login">
    <title>๋กœ๊ทธ์ธ ์‹คํŒจ</title>
</head>
<body>
<p>๋กœ๊ทธ์ธ์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•™๋ฒˆ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋‹ค์‹œ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.<br>
์ž ์‹œ ํ›„ ๋กœ๊ทธ์ธ ํ™”๋ฉด์œผ๋กœ ์ด๋™๋ฉ๋‹ˆ๋‹ค.</p>
</body>
</html>
cs

 

๋กœ๊ทธ์ธ ์‹คํŒจ ์‹œ 3์ดˆ ํ›„ ๋กœ๊ทธ์ธ ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•˜๋„๋ก <meta> ํƒœ๊ทธ์— refresh ์ •๋ณด๋ฅผ ๋‹ด์•˜๋‹ค.

๋กœ๊ทธ์ธ ์‹คํŒจ ๋ฌธ๊ตฌ ์ถœ๋ ฅ ํ›„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ 3์ดˆ ํ›„ /auth/login์„ ๋‹ค์‹œ ์š”์ฒญํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๊ฒŒ ๋œ๋‹ค.

 

4. ๋กœ๊ทธ์ธ ์ •๋ณด ์ถœ๋ ฅ

๋กœ๊ทธ์ธ์ด ์„ฑ๊ณตํ•˜๋ฉด ํ•™์ƒ ๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋˜๋„๋ก ํ•˜์˜€๋‹ค.

์ด์ œ ํ•™์ƒ ๋ชฉ๋ก์˜ ํ—ค๋” ๋ถ€๋ถ„์— ๋กœ๊ทธ์ธํ•œ ํ•™์ƒ์˜ ์ •๋ณด(์ด๋ฆ„)์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ• ๊ฒƒ์ด๋‹ค.

์•„๊นŒ ๋กœ๊ทธ์ธ ์„œ๋ธ”๋ฆฟ(LogInServlet.java)์—์„œ HttpSession์— ๋กœ๊ทธ์ธํ•œ ํ•™์ƒ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ด์•˜์œผ๋‹ˆ JSP์—์„œ ๊บผ๋‚ด ์“ฐ๋ฉด ๋œ๋‹ค.

 

Header.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    Student student = (Student) session.getAttribute("student");
%>
<div style="background-color: darkcyan; color: aliceblue; height: 20px; padding: 5px;">
    SPMS
    <% if(student == null) { %>
    <span style="float: right;">
        <a style="color: white;" href="<%=request.getContextPath()%>/auth/login">๋กœ๊ทธ์ธ</a>
    </span>
    <% } else {%>
    <span style="float: right;"><%=student.getStudentName()%>
        <a style="color: white;" href="<%=request.getContextPath()%>/auth/logout">๋กœ๊ทธ์•„์›ƒ</a>
    </span>
    <% } %>
</div>
cs

 

JSP์—์„œ๋Š” HttpSession์— ๋‚ด์žฅ ๊ฐ์ฒด session์„ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

session์„ ์‚ฌ์šฉํ•ด์„œ "student" ํ‚ค๋กœ ์ €์žฅ๋œ ํ•™์ƒ ์ธ์Šคํ„ด์Šค๋ฅผ ๊บผ๋‚ธ๋‹ค.

 

Student student = (Student) session.getAttribute("student");

 

Student ์ธ์Šคํ„ด์Šค์˜ getStudentName()์„ ํ˜ธ์ถœํ•ด์„œ ํ™”๋ฉด์— ์ถœ๋ ฅํ•œ๋‹ค.

๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•˜์„ ๋•Œ๋Š”(if(student == null)) ๋กœ๊ทธ์ธ ํผ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๋งํฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

5. ๋กœ๊ทธ์ธ ํ…Œ์ŠคํŠธ

๋กœ๊ทธ์ธ ๋งํฌ ํด๋ฆญ

 

์ •๋ณด ์ž…๋ ฅ ํ›„ ๋กœ๊ทธ์ธ

 

ํ•™์ƒ ๋ชฉ๋ก์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋˜๋ฉฐ ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž ์ •๋ณด์™€ ๋กœ๊ทธ์•„์›ƒ ๋งํฌ๊ฐ€ ํ‘œ์‹œ๋จ

 

6. ๋กœ๊ทธ์•„์›ƒ ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ

LogOutServlet.java

1
2
3
4
5
6
7
8
9
10
@WebServlet("/auth/logout")
public class LogOutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.invalidate();
 
        resp.sendRedirect("login");
    }
}
cs

 

๋กœ๊ทธ์•„์›ƒ์€ invalidate()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ HttpSession ๊ฐ์ฒด๋ฅผ ๋ฌดํšจํ™”์‹œ์ผœ์„œ ๊ตฌํ˜„ํ•œ๋‹ค.

์„ธ์…˜ ๊ฐ์ฒด๊ฐ€ ๋ฌดํšจํ™” ๋œ๋‹ค๋Š” ๊ฒƒ์€ HttpSession ๊ฐ์ฒด๊ฐ€ ์ œ๊ฑฐ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

 

HttpSession session = req.getSession();
session.invalidate();

 

์ด๋ ‡๊ฒŒ ๋ฌดํšจํ™”๋œ HttpSession ๊ฐ์ฒด๋Š” ์ƒˆ๋กœ์šด ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.

 

7. ๋กœ๊ทธ์•„์›ƒ ํ…Œ์ŠคํŠธ

๋กœ๊ทธ์•„์›ƒ ๋งํฌ ํด๋ฆญ

 

๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ, '์ทจ์†Œ' ํด๋ฆญ

 

ํ•™์ƒ ๋ชฉ๋ก์œผ๋กœ ์ด๋™, ๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์œผ๋ฏ€๋กœ '๋กœ๊ทธ์ธ' ๋งํฌ๊ฐ€ ์ถœ๋ ฅ๋จ


  • ๊ด€๋ จ ๊ธ€

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (5) ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ(HttpSession)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€