본문 바로가기
풀스택/css

다시시작

by woohyun22 2019. 2. 9.

emaillist 부터 다시시작


servlet-jsp활용 dao,vo활용 


jsp는 위에 dao,vo들을 import해주는 반면 jstl의 설정해주는것



jstl은 위에서 코드 선언이 필요하다.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

일단 emaillist부터 끝내고 guestbook에서 바뀐 부분을 체크


---emaillist


dao와 vo를 import한다.



emaillist 의 index


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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="com.douzon.guestbook.vo.GuestbookVo"%>
<%@page import="com.douzon.guestbook.dao.GuestbookDao"%>


//dao와 vo,list를 import해준다. 패키지명.java파일명
// jsp형식이고, 그냥 형태만알아두자.


<%
    GuestbookDao dao = new GuestbookDao();
    List<GuestbookVo> list = dao.getList();
%>


//다오 객체선언과 다오의 리스트를 가져왔다.


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>방명록</title>
</head>
<body>
    <form action="<%=request.getContextPath() %>/add.jsp" method="post">


//form 에서는 보낼방식과 어디로 보낼지 여기선 index에서 add.jsp로 적을 값을 보낸다.


    <table border="1" width="500">
        <tr>
            <td>이름</td><td><input type="text" name="name"></td>


//여기서 보내주는 값의 이름을 미리 정해준다. 이이름을 그대로 들고 add로 간다.

            <td>비밀번호</td><td><input type="password" name="pass"></td>
        </tr>
        <tr>
            <td colspan="4"><textarea name="content" cols="60" rows="5"></textarea></td>
        </tr>
        <tr>
            <td colspan="4" align="right"><input type="submit" value=" 확인 "></td>
        </tr>
    </table>
    </form>
    <%
        int totalCount = list.size();
        int index = 0;
        for( GuestbookVo vo : list ) {    
    %> 
        <br>
        <table width="510" border="1">
            <tr>
                <td>[<%=totalCount - index++ %>]</td>
                <td><%=vo.getName() %></td>
                <td><%=vo.getRegDate() %></td>
                <td><a href="<%=request.getContextPath() %>/deleteform.jsp?no=<%=vo.getNo() %>">삭제</a></td>
            </tr>
            <tr>
                <td colspan="4">
                <%=vo.getMessage().replaceAll("\n""<br>"%>
                </td>
            </tr>
        </table>
    <%
        }
    %>
</body>
</html>
cs


form안에서 submit type의 확인버튼을 누르면 add.jsp로 값을 전달하는듯하다.

밑의 토탈카운트는 방명록의 적혀진 순서를 나타낸다.  - 이거 다시 생각해보기

for문 돌려서 list를 다 보여준다. 

삭제누를시 deleteform.jsp로 넘어가고 vo의 no을 들고간다? - no은 고유값이므로(기본키) 다른값들과 비교하려면 이값을 꼭 들고 넘어가야된다.



add.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@page import="com.douzon.guestbook.dao.GuestbookDao"%>
<%@page import="com.douzon.guestbook.vo.GuestbookVo"%>
<%
    request.setCharacterEncoding( "utf-8" );
 
    String name = request.getParameter( "name" );
    String password = request.getParameter( "pass" );
    String content = request.getParameter( "content" );
    
    GuestbookVo vo = new GuestbookVo();
    vo.setName( name );
    vo.setPassword( password );
    vo.setMessage( content );
    
    new GuestbookDao().insert( vo );
    
    response.sendRedirect( request.getContextPath() );
    
%> 
 
cs


utf-8로 한글깨짐을 방지

request.getparameter로 받아온 name 값을 String name에 넣어줌

vo에 name을 set해준다.

dao의 insert 메서드에 vo를 넣어준다.

마지막의 response.sendredirect 는 페이지전환없이 넘겨주는것 페이지전환을 보여줄때는 forward를 쓴다.



deleteform.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>방명록</title>
</head>
<body>
    <form method="post" action="<%=request.getContextPath() %>/delete.jsp">
    <input type='hidden' name="no" value="<%=request.getParameter( "no" ) %>">
    <table>
        <tr>
            <td>비밀번호</td>
            <td><input type="password" name="password"></td>
            <td><input type="submit" value="확인"></td>
        </tr>
    </table>
    </form>
    <a href="<%=request.getContextPath() %>">메인으로 돌아가기</a>
</body>
</html>
cs


action에서 delete.jsp로 보내고 비밀번호 타입을 hidden으로 no값을 받는다.

add처럼 direct로 안보여주고 delete로 가서 지워줄껄로 예상 

request.getContextpath는 지정해준 주소(귀찮게 index.jsp가 있는 경로를 다적어줄 필요없이) 바로 보내준다. index로



delete.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@page import="com.douzon.guestbook.vo.GuestbookVo"%>
<%@page import="com.douzon.guestbook.dao.GuestbookDao"%>
<%
    String no = request.getParameter( "no" );
    String password = request.getParameter( "password" );
    
    GuestbookVo vo = new GuestbookVo();
    vo.setNo( Long.parseLong( no ) );
    vo.setPassword( password );
    
    new GuestbookDao().delete( vo );
    
    response.sendRedirect( request.getContextPath() );
%>
cs


no와 비밀번호를 같이 가져왔다? no는 얘만 가지고있는 고유의 값이다. 기본키

패스워드가 같아도 고유값은 모두 다르기때문에 둘의 값을 가져와서 같다면 삭제가 진행된다.

728x90

'풀스택 > css' 카테고리의 다른 글

css ex12~  (0) 2019.02.14
html/css/javascript  (0) 2019.02.11
그날그날 정리 1 jdbc dao와 vo  (0) 2019.01.21

댓글