[JSP] 게시판 등록 구현하기

 

jsp 회원등록,수정,삭제,리스트(CRUD) 로직 구현하기

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
else if (command.equals("/board/boardWriteAction.do")) {  // 작성페이지에서 글다 작성하고 글쓰기 등록버튼을 누르면 실행됨
            System.out.println("작성액션화면으로 들어옴~");
            
            
            
            // jar 로 이용해서 새로운 객체를 생성함...
            
            MultipartRequest multipartRequest = null;   // mutipatrequet= request 같은 역할인데 파일 업로드를 위해서 필요한것
            
            multipartRequest = new MultipartRequest(request,saveFullPath,sizeLimit,"UTF-8",new DefaultFileRenamePolicy());
            
            
            // request = 클라이언트 >> 서버 요청 servlet ...서블릿
            // saveFullPath => 저장경로 위치 - > 서버측에 파일이 저장될 경로
            // sizeLimit = > 파일 사이즈 - > 한번에 업로드 사이즈 크기
            // 언어 코드 = > UTF-8 - > 파일의 인코딩 방식.
            // new DefalutrenamePolicy()  == > 파일이름 중복처리를 위함
            
            
            
            // /board/boardWrite 에서 값이 넘어온  제목 , 내용 ,작성자를 request --> multipartRequest 수정
            
            String subject = multipartRequest.getParameter("subject");
            String content = multipartRequest.getParameter("content");
            String writer = multipartRequest.getParameter("writer");
            
            
        //    System.out.println(subject+";"+content+";"+writer);
            
            
            //열거자에 저장될 파일을 담는 객체 
            // Enumeration == > 데이터를 한번에 출력하게 도와줌..
            
            // 폼에서 전송된 파라미터 타입의 file 이 아닌 이름 들을  Enumeration =>리턴 ?
            Enumeration files = multipartRequest.getFileNames();
            
            // 실행순서
            // Enumneration = > 
            // hasMoreElements() = > Enumneration 내용이있는지 확인
            // nextElement(); = > 내용 값을 가져옴.
            
            
            String file = (String)files.nextElement();
            
            // 글쓰기페이지에 업로드한 파일이 서버에 업로된 파일의 이름을 가져온다.
            
            String fileName = multipartRequest.getFilesystemName(file);
            
            
            System.out.println("fileName =" +fileName);
            
            // 클라이언트가 업로드한 파일의 원본이름을 반환한다.
            
            String originalFileName = multipartRequest.getOriginalFileName(file);
            
            
            
            //  ip 주소 
            String ip = InetAddress.getLocalHost().getHostAddress();
            
            
            
            // 회원으로 로그인 한 사람의 세션 값  === > midx 기준
            
            
            HttpSession session = request.getSession();
            int midx = (int)session.getAttribute("midx");
            
            
            
            // 글쓰기 페이지 넘어온 값을 insertboard 에다가 데이터 집어 넣음.
            
            BoardDao bd = new BoardDao();
            int value = bd.insertBoard(subject, content, writer, ip, midx,fileName);
            
            if (value==1) {
                response.sendRedirect(request.getContextPath()+"/board/boardList.do");                
            }else {
                response.sendRedirect(request.getContextPath()+"/board/boardWrite.do");
            }            
cs
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
public int insertBoard(String subject,String content,String writer,String ip, int midx,String filename) {
        int value= 0;
        
        String sql="INSERT INTO BOARD(bidx,ORiginbidx,depth,level_,subject,content,writer,ip,midx,filename)"
                + "values(bidx_b_seq.NEXTVAL,bidx_b_seq.NEXTVAL,0,0,?,?,?,?,?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, subject);
            pstmt.setString(2, content);
            pstmt.setString(3, writer);
            pstmt.setString(4, ip);
            pstmt.setInt(5, midx);
            pstmt.setString(6, filename);
            value = pstmt.executeUpdate();
            
        } catch (SQLException e) {            
            e.printStackTrace();
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {                
                e.printStackTrace();
            }    
        }    
        
        return value;
    }
cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="damain.BoardVo" %> 
<%@page import="damain.PageMaker" %>
    
    <%
    
    ArrayList<BoardVo> alist = (ArrayList<BoardVo>)request.getAttribute("alist");
    
    PageMaker pm = (PageMaker)request.getAttribute("pm");
    
    
    %>
    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#btn{
float: right;
margin-right: 40px;
}
 
#btn1{
margin-left: 30px;
}
 
body{
display:flex;
text-align:center;
justify-content: center;
align-items: center;
 
}
 
#main{
margin-top: 150px;
 
}
 
</style>
 
<!-- FONT AWE -->
<script src="https://kit.fontawesome.com/6c060c00b1.js" crossorigin="anonymous"></script>
 
<!-- BOOT STRAP -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body>
 
 
 
 
  <div id="main">
        <h1 style="text-align: center; ">고객센터</h1>
        <hr><br>
        <button type="button" class="btn btn-primary" id="btn1" onclick="location.href='<%=request.getContextPath()%>/'">홈으로</button>
        <button type="button" class="btn btn-info" id="btn" onclick="location.href='<%=request.getContextPath()%>/board/boardWrite.do'">글쓰기</button>
        <form name="frm" action="<%=request.getContextPath() %>/board/boardList.do" method="post">
        <table style="text-align: right;margin-top: 30px;">
        <tr>
        <td width="1000px;">
           <select name="searchType">
           <option value="subject">제목</option>
           <option value="writer">작성자</option>
           </select>
         </td>
         <td>
         <input type="text" name="keyword" size="20">
         </td>
         <td>
         <input type="submit" name="submit" value="검색">
         </td>
        </tr>
    </table> 
        </form>
        
        
        <br><br>
        <table class="table table-hover" style="width: 1200px; text-align: center;" id="table1">
            <thead>
                <tr>
                    <th>번호</th>
                    <th>제목</th>
                    <th>글쓴이</th>
                    <th>작성일</th>
                    <th>조회수</th>
                </tr>
            </thead>
            
            <% for(BoardVo bv : alist) {  %>
            <tbody>
                <tr>
                    <td><%=bv.getBidx() %></td>
                    <td>
                    
                    <%
                    
                    for (int i=1; i<= bv.getLevel_(); i++) { 
                        out.println("&nbsp;&nbsp;");
                        if (i == bv.getLevel_()){
                            out.println("ㄴ");
                        }    
                    }        
                    %>
                    
                     
                    
                    <a href="<%=request.getContextPath()%>/board/boardContent.do?bidx=<%=bv.getBidx()%>"><%=bv.getSubject() %></a>
                    
                    </td>
                    
                    
                    <td><%=bv.getWriter() %></td>
                    <td><%=bv.getWriteday().substring(0,16) %></td>
                    <td><%=bv.getHit() %></td>
                </tr>
           
            </tbody>
            <% } %>
        </table>
          
 
 
 
  
<
        <br><br>
        <nav aria-label="Page navigation example">
            <ul class="pagination justify-content-center">
              <li class="page-item">
              
              <%  
              
              String keyword = pm.getScri().getKeyword();            // boardtotal 결과  키워드 가져오기
              String searchType = pm.getScri().getSearchType();      //   / /      결과  서치타입 가져오기
              
              
              
              
              if (pm.isPrev() == true){
                  out.println("<a class=page-link  href='"+request.getContextPath()+"/board/boardList.do?page="+(pm.getStartPage()-1)+"&keyword="+keyword+"&searchType="+searchType+"'>◀</a>");     }
              %>
              
              </li>
              <li class="page-item" style="display: flex;">
              <%
     
     for(int i = pm.getStartPage(); i<=pm.getEndPage(); i++){
         out.println("<a class=page-link href='"+request.getContextPath()+"/board/boardList.do?page="+i+"&keyword="+keyword+"&searchType"+searchType+"'>"+i+"</a>");
     }
     
               %>
              </li>
          
              <li class="page-item">
              
                <%
     
     if(pm.isNext()&&pm.getEndPage()>0){
         out.println("<a class=page-link href='"+request.getContextPath()+"/board/boardList.do?page="+(pm.getEndPage()+1)+"&keyword="+keyword+"&searchType="+searchType+"'></a>");
     }
     
               
                %>
                
              </li>
            </ul>
        </nav>
         
    </div>
</body>
</html>
 
<!-- class="page-link" -->
cs