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

[IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (6) ํšŒ์› ์‚ญ์ œ

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) - (6) ํšŒ์› ์‚ญ์ œ


[IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (6) ํšŒ์› ์‚ญ์ œ

๋“ฑ๋ก๋œ ํ•™์ƒ ์ค‘ ์„ ํƒํ•œ ํ•™์ƒ์„ ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

๊ด€๋ฆฌ์ž๋งŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋„๋ก ํ• ๊ฒƒ์ด๋‹ค.

 

1. ํ•™์ƒ ๋ชฉ๋ก JSP ๋ณ€๊ฒฝ

ํ•™์ƒ ์ •๋ณด ์•ž์— '์‚ญ์ œ' ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

StudentList.jsp

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
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    Student loginMember = (Student) session.getAttribute("student");
%>
<html>
<head>
    <title>ํ•™์ƒ ๋ชฉ๋ก</title>
</head>
<body>
<jsp:include page="/Header.jsp"/>
<h1>ํ•™์ƒ ๋ชฉ๋ก</h1>
<p><a href="add">์‹ ๊ทœ ํ•™์ƒ</a></p>
<%--<jsp:useBean id="students" scope="request" class="java.util.ArrayList" type="java.util.List<com.atoz_develop.spms.vo.Student>" />--%>
<%
    List<Student> students = (ArrayList<Student>)request.getAttribute("students");
    for(Student student: students) {
        if(loginMember != null && "admin".equals(loginMember.getStudentNo())) {
%>
<input type="button" value="์‚ญ์ œ" onClick="location.href='delete?student_no=<%=student.getStudentNo()%>'" />
<%
        }
%>
<%=student.getStudentNo()%>,
<%=student.getDepartment()%>,
<a href="update?student_no=<%=student.getStudentNo()%>"><%=student.getStudentName()%></a>,
<%=student.getGrade()%>,
<%=student.getGender()%>,
<%=student.getAge()%>,
<%=student.getPhoneNumber()%>,
<%=student.getAddress()%><br>
<%
    }
%>
<jsp:include page="/Tail.jsp"/>
</body>
</html>
cs

 

session์—์„œ ๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์„œ admin์ด๋ฉด ์‚ญ์ œ ๋ฒ„ํŠผ์„ ์ถœ๋ ฅํ•œ๋‹ค.

์‚ญ์ œ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ /student/delete๋กœ GET ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

์‚ญ์ œํ•  ํ•™์ƒ์˜ ํ•™๋ฒˆ(student_no)์„ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํ•จ๊ป˜ ๋ณด๋‚ธ๋‹ค.

 

ํ•™์ƒ ์ •๋ณด ์ˆ˜์ • JSP์—๋„ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

StudentUpdateForm.jsp

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
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    Student loginMember = (Student) session.getAttribute("student");
    Student student = (Student) request.getAttribute("student");
%>
<html>
<head>
    <title>ํ•™์ƒ ์ •๋ณด</title>
</head>
<body>
<h1>ํ•™์ƒ์ •๋ณด</h1>
<form action='update' method='post'>
    ํ•™๋ฒˆ: <input type='text' name='student_no' value='<%=student.getStudentNo()%>' readonly><br>
    ํ•™๊ณผ: <input type='text' name='department' value='<%=student.getDepartment()%>'><br>
    ์ด๋ฆ„: <input type='text' name='student_name' value='<%=student.getStudentName()%>'><br>
    ํ•™๋…„: <input type='text' name='grade' value='<%=student.getGrade()%>' readonly><br>
    ์„ฑ๋ณ„: <input type='text' name='gender' value='<%=student.getGender()%>' readonly><br>
    ๋‚˜์ด: <input type='text' name='age' value='<%=student.getAge()%>' readonly><br>
    ์ „ํ™”๋ฒˆํ˜ธ: <input type='text' name='phone_number' value='<%=student.getPhoneNumber()%>'><br>
    ์ฃผ์†Œ: <input type='text' name='address' value='<%=student.getAddress()%>'><br>
    <input type='submit' value='์ˆ˜์ •'>
    <%
        if(loginMember != null && "admin".equals(loginMember.getStudentNo())) {
    %>
    <input type="button" value="์‚ญ์ œ" onClick="location.href='delete?student_no=<%=student.getStudentNo()%>'" />
    <%
        }
    %>
    <input type='button' value='์ทจ์†Œ' onClick='location.href="list"'>
</form>
</body>
</html>
cs

 

์ผ๋ฐ˜ ๊ณ„์ • ๋กœ๊ทธ์ธ ๋˜๋Š” ๋ฏธ๋กœ๊ทธ์ธ ์ƒํƒœ

 

๊ด€๋ฆฌ์ž ๊ณ„์ • ๋กœ๊ทธ์ธ

 

์ผ๋ฐ˜ ๊ณ„์ • ๋กœ๊ทธ์ธ ๋˜๋Š” ๋ฏธ๋กœ๊ทธ์ธ ์ƒํƒœ

 

๊ด€๋ฆฌ์ž ๊ณ„์ • ๋กœ๊ทธ์ธ

 

2. ์‚ญ์ œ ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ

StudentDeleteServlet.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
@WebServlet("/student/delete")
public class StudentDeleteServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement pstmt = null;
 
        ServletContext sc = req.getServletContext();
 
        try {
            conn = (Connection) sc.getAttribute("conn");
            pstmt = conn.prepareStatement(
                    "DELETE FROM STUDENT WHERE STUDENT_NO = ?"
            );
            pstmt.setString(1, req.getParameter("student_no"));
            if(pstmt.executeUpdate() > 0) {
                resp.sendRedirect("list");
            } else {
                throw new SQLException();
            }
        } catch (SQLException e) {
            req.setAttribute("error", e);
            RequestDispatcher rd = req.getRequestDispatcher("/Error.jsp");
            rd.forward(req, resp);
        } finally {
            try {
                if(pstmt != null) pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
cs

 

์‚ญ์ œ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ํšŒ์› ๋ชฉ๋ก์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๊ณ  ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ Error.jsp ํŽ˜์ด์ง€๋กœ ํฌ์›Œ๋”ฉํ•˜๋„๋ก ํ•˜์˜€๋‹ค.


  • ๊ด€๋ จ ๊ธ€

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

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

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

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

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

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (6) ํšŒ์› ์‚ญ์ œ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€