JSP Servlet 기초(25) 게시판 만들기
- JSP
- 2018. 2. 3. 19:22
저번 포스팅에서 말했듯이
(16)포스팅에서 만든 회원인증 프로젝트와
저번 포스팅에서 다운받은 부트스트랩으로
게시판을 만들어 보겠습니다. 이번 게시판은
MVC 모델2방식으로 만들겠습니다.
먼저 부트스트랩 적용한 login 화면과
join화면 main화면 보여드리겠습니다.
css에 대한 설명은 하지않겠습니다
또한 게시판은 제 블로그 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width", initial-scale="1"> <link rel="stylesheet" href="css/bootstrap.css"> <title>JSP 게시판</title> </head> <body> <nav class="navbar navbar-default"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle=:collapse data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="main.jsp">JSP 게시판 웹</a> </div> <div class="collapse navbar-collapse" id="bs="#bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="main.jsp">메인</a></li> <li><a href="boardList.do">게시판</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">접속하기<span class="caret"></span></a> <ul class="dropdown-menu"> <li class="active"><a href="login.jsp">로그인</a></li> <li><a href="join.jsp">회원가입</a></li> </ul> </li> </ul> </div> </nav> <div class="container"> <div class="col-lg-4"></div> <div class="col-lg-4"> <div class="jumbotron" style="padding-top: 20px;"> <form method="post" action="loginGo.jsp"> <h3 style="text-align: center;">로그인 화면</h3> <div class="form-group"> <input type="text" class="form-control" placeholder="아이디" name="id" maxlength="20"> </div> <div class="form-group"> <input type="password" class="form-control" placeholder="비밀번호" name="pw" maxlength="20"> </div> <input type="submit" class="btn btn-primary form-control" value="로그인"> </form> </div> <div class="col-lg-4"></div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> | 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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width", initial-scale="1"> <link rel="stylesheet" href="css/bootstrap.css"> <title>JSP 게시판</title> <script language="JavaScript" src="members.js" ></script> </head> <body> <nav class="navbar navbar-default"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle=:collapse data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="main.jsp">JSP 게시판 웹</a> </div> <div class="collapse navbar-collapse" id="bs="#bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="main.jsp">메인</a></li> <li><a href="board.jsp">게시판</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">접속하기<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="login.jsp">로그인</a></li> <li class="active"><a href="join.jsp">회원가입</a></li> </ul> </li> </ul> </div> </nav> <div class="container"> <div class="col-lg-4"></div> <div class="col-lg-4"> <div class="jumbotron" style="padding-top: 20px;"> <form method="post" action="joinGo.jsp" name="reg_frm"> <h3 style="text-align: center;">회원가입 화면</h3> <div class="form-group"> <input type="text" class="form-control" placeholder="아이디" name="id" maxlength="20"> </div> <div class="form-group"> <input type="password" class="form-control" placeholder="비밀번호" name="pw" maxlength="20"> </div> <div class="form-group"> <input type="password" class="form-control" placeholder="비밀번호확인" name="pw_check" maxlength="20"> </div> <div class="form-group"> <input type="text" class="form-control" placeholder="이름" name="name" maxlength="20"> </div> <div class="form-group"> <input type="email" class="form-control" placeholder="이메일" name="eMail" maxlength="20"> </div> <div class="form-group"> <input type="text" class="form-control" placeholder="주소" name="address" maxlength="50"> </div> <input type="button" class="btn btn-primary form-control" value="회원가입" onclick="infoConfirm()"> </form> </div> <div class="col-lg-4"></div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> | 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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.PrintWriter"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width", initial-scale="1"> <link rel="stylesheet" href="css/bootstrap.css"> <title>JSP 게시판</title> </head> <body> <%String id = null; if(session.getAttribute("id") != null){ id = (String)session.getAttribute("id"); } %> <nav class="navbar navbar-default"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle=:collapse data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="main.jsp">JSP 게시판 웹</a> </div> <div class="collapse navbar-collapse" id="bs="#bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="main.jsp">메인</a></li> <li><a href="boardList.do">게시판</a></li> </ul> <% if(id == null) { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">접속하기<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="login.jsp">로그인</a></li> <li><a href="join.jsp">회원가입</a></li> </ul> </li> </ul> <% } else { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">회원관리<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="logout.jsp">로그아웃</a></li> <li><a href="modify.jsp">회원정보수정</a></li> </ul> </li> </ul> <% } %> </div> </nav> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> | cs |
다음 게시판을 위해
FrontController 역할 을 할 서블릿을 하나 만들어줍니다
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 | package com.jsp.ex; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jsp.ex.command.BCommand; import com.jsp.ex.command.BContentCommand; import com.jsp.ex.command.BDeleteCommand; import com.jsp.ex.command.BListCommand; import com.jsp.ex.command.BModifyCommand; import com.jsp.ex.command.BWriteCommand; @WebServlet("*.do") public class FrontCon extends HttpServlet { private static final long serialVersionUID = 1L; public FrontCon() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { actionDo(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { actionDo(request, response); } private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("actionDo"); request.setCharacterEncoding("UTF-8"); String viewPage = null; BCommand command = null; System.out.println("모디파이"); String uri = request.getRequestURI(); String conPath = request.getContextPath(); String com = uri.substring(conPath.length()); System.out.println("모디파이2"); if(com.equals("/write_view.do")) { viewPage = "write_view.jsp"; } else if(com.equals("/write.do")) { command = new BWriteCommand(); command.execute(request, response); viewPage = "boardList.do"; } else if(com.equals("/boardList.do")) { command = new BListCommand(); command.execute(request, response); viewPage = "boardList.jsp"; } else if(com.equals("/boardViewContent.do")){ command = new BContentCommand(); command.execute(request, response); viewPage = "boardViewContent.jsp"; } else if(com.equals("/boardModify.do")) { command = new BContentCommand(); command.execute(request, response); viewPage = "boardModify.jsp"; } else if(com.equals("/boardContentModify.do")) { command = new BModifyCommand(); command.execute(request, response); viewPage = "boardList.do"; } else if(com.equals("/delete.do")) { command = new BDeleteCommand(); command.execute(request, response); viewPage = "boardList.do"; } RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage); dispatcher.forward(request, response); } } | cs |
먼저 확장자패턴으로 넘어온 값들을
저번 포스팅에서 알아본 command패턴을 이용해
각 클래스로 보내줍니다.
다음 74line 에서는 RequestDispatcher 클래스를 이용해
viewPage를 포워딩 시켜줍니다
다음 패키지를 새로 만들어서
BDto.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 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 | package com.jsp.ex.dto; import java.sql.Timestamp; public class BDto { int bId; String bName; String bTitle; String bContent; Timestamp bDate; int bHit; int bGroup; int bStep; int bIndent; public BDto() { } public BDto(int bId, String bName, String bTitle, String bContent, Timestamp bDate, int bHit, int bGroup, int bStep, int bIndent) { this.bId = bId; this.bName = bName; this.bTitle = bTitle; this.bContent = bContent; this.bDate = bDate; this.bHit = bHit; this.bGroup = bGroup; this.bStep = bStep; this.bIndent = bIndent; } public int getbId() { return bId; } public void setbId(int bId) { this.bId = bId; } public String getbName() { return bName; } public void setbName(String bName) { this.bName = bName; } public String getbTitle() { return bTitle; } public void setbTitle(String bTitle) { this.bTitle = bTitle; } public String getbContent() { return bContent; } public void setbContent(String bContent) { this.bContent = bContent; } public Timestamp getbDate() { return bDate; } public void setbDate(Timestamp bDate) { this.bDate = bDate; } public int getbHit() { return bHit; } public void setbHit(int bHit) { this.bHit = bHit; } public int getbGroup() { return bGroup; } public void setbGroup(int bGroup) { this.bGroup = bGroup; } public int getbStep() { return bStep; } public void setbStep(int bStep) { this.bStep = bStep; } public int getbIndent() { return bIndent; } public void setbIndent(int bIndent) { this.bIndent = bIndent; } } | cs |
bDate는 작성날짜 입니다.
Hit는 조회수이고
Group, Step, Indent는 답글을위해
만들었습니다.
다음 새로운 패키지를 하나 만들어서
BDao.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 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | package com.jsp.ex.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; import java.util.ArrayList; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import com.jsp.ex.dto.BDto; public class BDao { DataSource dataSource; public BDao() { try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("java:comp/env/jdbc/Oracle11g"); } catch (Exception e) { e.printStackTrace(); } } public void write(String bName, String bTitle, String bContent) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = dataSource.getConnection(); String query = "insert into mvc_board (bId, bName, bTitle, bContent, bHit, bGroup, bStep, bIndent) values (mvc_board_seq.nextval, ?, ?, ?, 0, mvc_board_seq.currval, 0, 0 )"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, bName); preparedStatement.setString(2, bTitle); preparedStatement.setString(3, bContent); int rn = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public ArrayList<BDto> list() { ArrayList<BDto> dtos = new ArrayList<BDto>(); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = dataSource.getConnection(); String query = "select bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent from mvc_board order by bGroup desc, bStep asc"; preparedStatement = connection.prepareStatement(query); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { int bId = resultSet.getInt("bId"); String bName = resultSet.getString("bName"); String bTitle = resultSet.getString("bTitle"); String bContent = resultSet.getString("bContent"); Timestamp bDate = resultSet.getTimestamp("bDate"); int bHit = resultSet.getInt("bHit"); int bGroup = resultSet.getInt("bGroup"); int bStep = resultSet.getInt("bStep"); int bIndent = resultSet.getInt("bIndent"); BDto dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent); dtos.add(dto); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(resultSet != null) resultSet.close(); if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } return dtos; } public BDto contentView(String strID) { upHit(strID); BDto dto = null; Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = dataSource.getConnection(); String query = "select * from mvc_board where bId = ?"; preparedStatement = connection.prepareStatement(query); preparedStatement.setInt(1, Integer.parseInt(strID)); resultSet = preparedStatement.executeQuery(); if(resultSet.next()) { int bId = resultSet.getInt("bId"); String bName = resultSet.getString("bName"); String bTitle = resultSet.getString("bTitle"); String bContent = resultSet.getString("bContent"); Timestamp bDate = resultSet.getTimestamp("bDate"); int bHit = resultSet.getInt("bHit"); int bGroup = resultSet.getInt("bGroup"); int bStep = resultSet.getInt("bStep"); int bIndent = resultSet.getInt("bIndent"); dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(resultSet != null) resultSet.close(); if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } return dto; } public void modify(String bId, String bName, String bTitle, String bContent) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = dataSource.getConnection(); String query = "update mvc_board set bName = ?, bTitle = ?, bContent = ? where bId = ?"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, bName); preparedStatement.setString(2, bTitle); preparedStatement.setString(3, bContent); preparedStatement.setInt(4, Integer.parseInt(bId)); int rn = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public void delete(String bId) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = dataSource.getConnection(); String query = "delete from mvc_board where bId = ?"; preparedStatement = connection.prepareStatement(query); preparedStatement.setInt(1, Integer.parseInt(bId)); int rn = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public BDto reply_view(String str) { BDto dto = null; Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = dataSource.getConnection(); String query = "select * from mvc_board where bId = ?"; preparedStatement = connection.prepareStatement(query); preparedStatement.setInt(1, Integer.parseInt(str)); resultSet = preparedStatement.executeQuery(); if(resultSet.next()) { int bId = resultSet.getInt("bId"); String bName = resultSet.getString("bName"); String bTitle = resultSet.getString("bTitle"); String bContent = resultSet.getString("bContent"); Timestamp bDate = resultSet.getTimestamp("bDate"); int bHit = resultSet.getInt("bHit"); int bGroup = resultSet.getInt("bGroup"); int bStep = resultSet.getInt("bStep"); int bIndent = resultSet.getInt("bIndent"); dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } return dto; } public void reply(String bId, String bName, String bTitle, String bContent, String bGroup, String bStep, String bIndent) { replyShape(bGroup, bStep); Connection connection = null; PreparedStatement preparedStatement = null; try { connection = dataSource.getConnection(); String query = "insert into mvc_board (bId, bName, bTitle, bContent, bGroup, bStep, bIndent) values (mvc_board_seq.nextval, ?, ?, ?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, bName); preparedStatement.setString(2, bTitle); preparedStatement.setString(3, bContent); preparedStatement.setInt(4, Integer.parseInt(bGroup)); preparedStatement.setInt(5, Integer.parseInt(bStep) + 1); preparedStatement.setInt(6, Integer.parseInt(bIndent) + 1); int rn = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } } private void replyShape( String strGroup, String strStep) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = dataSource.getConnection(); String query = "update mvc_board set bStep = bStep + 1 where bGroup = ? and bStep > ?"; preparedStatement = connection.prepareStatement(query); preparedStatement.setInt(1, Integer.parseInt(strGroup)); preparedStatement.setInt(2, Integer.parseInt(strStep)); int rn = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } } private void upHit( String bId) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = dataSource.getConnection(); String query = "update mvc_board set bHit = bHit + 1 where bId = ?"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, bId); int rn = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } | cs |
Dao 에서는 글작성, 글수정, 글보기, 글삭제
등의 기능들을 위해 DB에 접근합니다.
다음 Command 파일들을 넣을 패키지를 만들어주고
인터페이스를 만들어줍니다
1 2 3 4 5 6 7 8 9 10 | package com.jsp.ex.command; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public interface BCommand { void execute(HttpServletRequest request, HttpServletResponse response); } | cs |
이제 글내용을 보여줄 BContentCommand 만들어줍니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.jsp.ex.command; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jsp.ex.dao.BDao; import com.jsp.ex.dto.BDto; public class BContentCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { String bId = request.getParameter("bId"); BDao dao = new BDao(); BDto dto = dao.contentView(bId); request.setAttribute("content_view", dto); } } | cs |
글삭제를 위한 커맨드도 만들어줍니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.jsp.ex.command; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jsp.ex.dao.BDao; public class BDeleteCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { String bId = request.getParameter("bId"); BDao dao = new BDao(); dao.delete(bId); } } | cs |
글 목록을 보여줄 커맨드도 만듭니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.jsp.ex.command; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jsp.ex.dao.BDao; import com.jsp.ex.dto.BDto; public class BListCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { BDao dao = new BDao(); ArrayList<BDto> dtos = dao.list(); request.setAttribute("list", dtos); } } | cs |
다음 글 수정을 위한 모디파이 커맨드 만들어줍니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.jsp.ex.command; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jsp.ex.dao.BDao; public class BModifyCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { String bId = request.getParameter("bId"); String bName = request.getParameter("bName"); String bTitle = request.getParameter("bTitle"); String bContent = request.getParameter("bContent"); BDao dao = new BDao(); dao.modify(bId, bName, bTitle, bContent); } } | cs |
마지막으로 글쓰기를 위한 커맨드 만들어줍니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.jsp.ex.command; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jsp.ex.dao.BDao; public class BWriteCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { String bName = request.getParameter("bName"); String bTitle = request.getParameter("bTitle"); String bContent = request.getParameter("bContent"); BDao dao = new BDao(); dao.write(bName, bTitle, bContent); } } | cs |
다음 jsp 화면을 만들어줍니다
먼저 게시글들의 리스트를 뽑아올 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 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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.PrintWriter"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <meta name="viewport" content="width=device-width", initial-scale="1"> <link rel="stylesheet" href="css/bootstrap.css"> <title>JSP 게시판</title> </head> <body> <%String id = null; if(session.getAttribute("id") != null){ id = (String)session.getAttribute("id"); } %> <nav class="navbar navbar-default"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle=:collapse data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="main.jsp">JSP 게시판 웹</a> </div> <div class="collapse navbar-collapse" id="bs="#bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="main.jsp">메인</a></li> <li class="active"><a href="boardList.jsp">게시판</a></li> </ul> <% if(id == null) { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">접속하기<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="login.jsp">로그인</a></li> <li><a href="join.jsp">회원가입</a></li> </ul> </li> </ul> <% } else { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">회원관리<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="logout.jsp">로그아웃</a></li> </ul> </li> </ul> <% } %> </div> </nav> <div class="container"> <div class="row"> <table class="table table-striped" style="text-align: center; border:1px solid #dddddd"> <thead> <tr> <th style="baclground-clolr:#eeeeee; text-align: center;">번호</th> <th style="baclground-clolr:#eeeeee; text-align: center;">작성자</th> <th style="baclground-clolr:#eeeeee; text-align: center;">제목</th> <th style="baclground-clolr:#eeeeee; text-align: center;">작성일</th> <th style="baclground-clolr:#eeeeee; text-align: center;">조회수</th> </tr> </thead> <tbody> <c:forEach items="${list}" var="dto"> <tr> <td>${dto.bId}</td> <td>${dto.bName}</td> <td><a href="boardViewContent.do?bId=${dto.bId}">${dto.bTitle}</a></td></td> <td>${dto.bDate}</td> <td>${dto.bHit}</td> </tr> </c:forEach> </tbody> </table> <a href="boardWrite.jsp" class="btn btn-primary pull-right">글쓰기</a> </div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> | cs |
다음 글수정을 위한 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 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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.PrintWriter"%> <%@page import="com.jsp.ex.MemberDTO"%> <%@page import="com.jsp.ex.MemberDAO"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <meta name="viewport" content="width=device-width", initial-scale="1"> <link rel="stylesheet" href="css/bootstrap.css"> <title>JSP 게시판</title> </head> <body> <% String id = (String)session.getAttribute("id"); MemberDAO dao = MemberDAO.getInstance(); MemberDTO dto = dao.getMember(id); System.out.println(id); %> <nav class="navbar navbar-default"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle=:collapse data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="main.jsp">JSP 게시판 웹</a> </div> <div class="collapse navbar-collapse" id="bs="#bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="main.jsp">메인</a></li> <li class="active"><a href="boardList.jsp">게시판</a></li> </ul> <% if(id == null) { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">접속하기<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="login.jsp">로그인</a></li> <li><a href="join.jsp">회원가입</a></li> </ul> </li> </ul> <% } else { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">회원관리<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="logout.jsp">로그아웃</a></li> </ul> </li> </ul> <% } %> </div> </nav> <div class="container"> <div class="row"> <form action="boardContentModify.do" method="post"> <input type="hidden" name="bId" value="${content_view.bId}"> <input type="hidden" name="bName" value="${content_view.bName}"> <table class="table table-striped" style="text-align: center; border:1px solid #dddddd"> <thead> <tr> <th colspan="2" style="baclground-clolr:#eeeeee; text-align: center;">게시판 글 수정</th> </tr> </thead> <tbody> <tr> <td>번호</td> <td colspan="2">${content_view.bId}</td> </tr> <tr> <td>조회수</td> <td colspan="2">${content_view.bHit}</td> </tr> <tr> <td style="width:20%;">글제목</td> <td colspan="2"><input type="text" class="form-control" value="${content_view.bTitle}" name="bTitle" maxlength="50"></td> </tr> <tr> <td>작성자</td> <td colspan="2">${content_view.bName}</td> </tr> <tr> <td>작성 일자</td> <td colspan="2">${content_view.bDate}</td> </tr> <tr> <td>내용</td> <td><textarea class="form-control" name="bContent" maxlength="1000" style="height: 350px;">${content_view.bContent}</textarea></td> </tr> </tbody> </table> <a href="boardList.do" class="btn btn-primary">목록</a> <input type="submit" class="btn btn-primary pull-right" value="수정"> </form> </div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> | cs |
이제 글작성을 위한 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 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 | <%@page import="com.jsp.ex.MemberDTO"%> <%@page import="com.jsp.ex.MemberDAO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.PrintWriter"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <meta name="viewport" content="width=device-width", initial-scale="1"> <link rel="stylesheet" href="css/bootstrap.css"> <title>JSP 게시판</title> </head> <body> <% String id = (String)session.getAttribute("id"); MemberDAO dao = MemberDAO.getInstance(); MemberDTO dto = dao.getMember(id); System.out.println(id); %> <nav class="navbar navbar-default"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle=:collapse data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="main.jsp">JSP 게시판 웹</a> </div> <div class="collapse navbar-collapse" id="bs="#bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="main.jsp">메인</a></li> <li class="active"><a href="boardList.jsp">게시판</a></li> </ul> <% if(id == null) { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">접속하기<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="login.jsp">로그인</a></li> <li><a href="join.jsp">회원가입</a></li> </ul> </li> </ul> <% } else { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">회원관리<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="logout.jsp">로그아웃</a></li> </ul> </li> </ul> <% } %> </div> </nav> <div class="container"> <div class="row"> <form action="write.do" method="post"> <table class="table table-striped" style="text-align: center; border:1px solid #dddddd"> <thead> <tr> <th colspan="2" style="baclground-clolr:#eeeeee; text-align: center;">게시판 글쓰기 양식</th> </tr> </thead> <tbody> <tr> <td><input type="text" value=<%=id%> readonly class="form-control" name="bName" maxlength="50"></td> </tr> <tr> <td><input type="text" class="form-control" placeholder="글 제목" name="bTitle" maxlength="50"></td> </tr> <tr> <td><textarea class="form-control" placeholder="글 내용" name="bContent" maxlength="1000" style="height: 350px;"></textarea></td> </tr> </tbody> </table> <input type="submit" class="btn btn-primary pull-right" value="글쓰기"> </form> </div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> | cs |
마지막으로 글 내용을 보여줄 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 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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.PrintWriter"%> <%@page import="com.jsp.ex.MemberDTO"%> <%@page import="com.jsp.ex.MemberDAO"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <meta name="viewport" content="width=device-width", initial-scale="1"> <link rel="stylesheet" href="css/bootstrap.css"> <title>JSP 게시판</title> </head> <body> <% String id = (String)session.getAttribute("id"); MemberDAO dao = MemberDAO.getInstance(); MemberDTO dto = dao.getMember(id); System.out.println(id); %> <nav class="navbar navbar-default"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle=:collapse data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="main.jsp">JSP 게시판 웹</a> </div> <div class="collapse navbar-collapse" id="bs="#bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="main.jsp">메인</a></li> <li class="active"><a href="boardList.jsp">게시판</a></li> </ul> <% if(id == null) { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">접속하기<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="login.jsp">로그인</a></li> <li><a href="join.jsp">회원가입</a></li> </ul> </li> </ul> <% } else { %> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">회원관리<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="logout.jsp">로그아웃</a></li> </ul> </li> </ul> <% } %> </div> </nav> <div class="container"> <div class="row"> <form action="write.do" method="post"> <table class="table table-striped" style="text-align: center; border:1px solid #dddddd"> <thead> <tr> <th colspan="2" style="baclground-clolr:#eeeeee; text-align: center;">게시판 글 보기</th> </tr> </thead> <tbody> <tr> <td>번호</td> <td colspan="2">${content_view.bId}</td> </tr> <tr> <td>조회수</td> <td colspan="2">${content_view.bHit}</td> </tr> <tr> <td style="width:20%;">글제목</td> <td colspan="2">${content_view.bTitle}</td> </tr> <tr> <td>작성자</td> <td colspan="2">${content_view.bName}</td> </tr> <tr> <td>작성 일자</td> <td colspan="2">${content_view.bDate}</td> </tr> <tr> <td>내용</td> <td colspan="2" style="min-height:200px; text-align:Left;">${content_view.bContent}</td> </tr> </tbody> </table> <a href="boardList.do" class="btn btn-primary">목록</a> <c:set var="str" value="${content_view.bName}"/> <% String str = (String)pageContext.getAttribute("str") ; System.out.println(str); if(str.equals(id)){ %> <a href="boardModify.do?bId=${content_view.bId}" class="btn btn-primary">수정</a> <a href="delete.do?bId=${content_view.bId}" class="btn btn-primary">삭제</a> <% } %> </form> </div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> | cs |
글내용 보여주는 jsp 에서는 가장 중요한것이
글 수정과 글 삭제버튼을 만드는 것입니다
글 수정과 글삭제는 작성자만 할수 있도록
해야 하기 때문에 현재 로그인 되어있는 세션 id값과
글 작성자의 id 값이 같아야만
글삭제와 글 수정 버튼이 보입니다.
115line 에서 JSTL 변수를 JSP 변수로
변환 시켜줍니다.
답글 커맨드는 파일 첨부만 하겠습니다.
'JSP' 카테고리의 다른 글
부트스트랩 템플릿 사용하기 (2) | 2018.03.14 |
---|---|
JSP - JSTL 변수를 JSP 변수로 (0) | 2018.02.03 |
JSP Servlet 기초(24) 게시판 만들기(부트스트랩 설치) (1) | 2018.02.03 |
JSP Servlet 기초(23) MVC패턴의 이해 (0) | 2018.02.03 |
JSP Servlet 기초(22) Command패턴 (0) | 2018.02.03 |