λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
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) νšŒμ› μ‚­μ œ

λ°˜μ‘ν˜•

λŒ“κΈ€