[JSP][동빈나] JSP 게시판 만들기 강좌 10강 - 글쓰기 기능 구현하기 (JSP Advanced Development Tutorial #10) // 다시ㄴㄴㄴ
카테고리 없음 2018. 5. 1. 04:19 |write.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 | <%@ 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 userID = null; // 로그인이 된 사람들은 로그인정보를 담을 수 있도록한다 if (session.getAttribute("userID") != null) { userID = (String)session.getAttribute("userID"); } %> <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-expand="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-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="main.jsp">메인</a></li> <li class="active"><a href="bbs.jsp">게시판</a></li> </ul> <% // 접속하기는 로그인이 되어있지 않은 경우만 나오게한다 if(userID == 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="loginAction.jsp">로그아웃</a></li> </ul> </li> </ul> <% } %> </div> </nav> <div class="container"> <div class="row"> <form method="post" action="writeAction.jsp"> <table class="table table-striped" style="text-align:center; border:1px solid #dddddd"> <thead> <tr> <th colspan="2" style="background-color:#eeeeee; text-align:center;">게시판 글쓰기 양식</th> </tr> </thead> <tbody> <tr> <td><input type="text" class="form-control" placeholder="글 제목" name="bbsTitle" maxlength="50" ></td> </tr> <tr> <td><textarea class="form-control" placeholder="글 내용" name="bbsContent" maxlength="2048" 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 |
BbsDAO.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 | package bbs; import java.sql.*; public class BbsDAO { private Connection conn; // DB에 접근하는 객체 private ResultSet rs; // DB data를 담을 수 있는 객체 (Ctrl + shift + 'o') -> auto import public BbsDAO(){ try { String dbURL = "jdbc:mysql://localhost:3306/BBS"; String dbID = "root"; String dbPassword = "1234"; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(dbURL, dbID, dbPassword); } catch (Exception e) { e.printStackTrace(); } } public String getDate() // 현재시간을 넣어주기위해 { String SQL = "SELECT NOW()"; // 현재시간을 나타내는 mysql try { PreparedStatement pstmt = conn.prepareStatement(SQL); rs = pstmt.executeQuery(); if(rs.next()) { return rs.getString(1); } } catch (Exception e) { e.printStackTrace(); } return ""; // 데이터베이스 오류 } public int getNext() { String SQL = "SELECT bbsID FROM BBS ORDER BY bbsID DESC"; // 내림차순으로 가장 마지막에 쓰인 것을 가져온다 try { PreparedStatement pstmt = conn.prepareStatement(SQL); rs = pstmt.executeQuery(); if(rs.next()) { return rs.getInt(1) + 1; // 그 다음 게시글의 번호 } return 1; // 첫 번째 게시물인 경우 } catch (Exception e) { e.printStackTrace(); } return -1; // 데이터베이스 오류 } public int write(String bbsTitle, String userID, String bbsContent) { String SQL = "INSERT INTO BBS VALUES (?,?,?,?,?,?)"; try { PreparedStatement pstmt = conn.prepareStatement(SQL); pstmt.setInt(1, getNext()); pstmt.setString(2, bbsTitle); pstmt.setString(3, userID); pstmt.setString(4, getDate()); pstmt.setString(5, bbsContent); pstmt.setInt(6, 1); return pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } return -1; // 데이터베이스 오류 } } | cs |
writeAction.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 | <!-- 실제로 글쓰기를 눌러서 만들어주는 Action페이지 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="bbs.BbsDAO" %> <%@ page import="java.io.PrintWriter" %> <!-- 자바스크립트 문장사용 --> <% request.setCharacterEncoding("UTF-8"); %> <!-- 건너오는 모든 파일을 UTF-8로 --> <jsp:useBean id="bbs" class="bbs.Bbs" scope="page"/> <jsp:setProperty name="bbs" property="bbsTitle" /> <jsp:setProperty name="bbs" property="bbsContent" /> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP게시판 웹사이트</title> </head> <body> <% String userID = null; // 로그인 된 사람은 회원가입페이지에 들어갈수 없다 if(session.getAttribute("userID") != null ) { userID = (String) session.getAttribute("userID"); } if(userID != null) { PrintWriter script = response.getWriter(); script.println("<script>"); script.println("alert('로그인을 하세요')"); script.println("location.href = 'login.jsp'"); script.println("</script>"); } else { if(bbs.getBbsTitle() == null || bbs.getBbsContent() == null) { PrintWriter script = response.getWriter(); script.println("<script>"); script.println("alert('입력이 안된 사항이 있습니다.')"); script.println("history.back()"); script.println("</script>"); } else { BbsDAO bbsDAO = new BbsDAO(); int result = bbsDAO.write(bbs.getBbsTitle(), userID, bbs.getBbsContent()); if(result == -1){ // 글쓰기에 실패했을 경우 PrintWriter script = response.getWriter(); //하나의 스크립트 문장을 넣을 수 있도록. script.println("<script>"); script.println("alert('글쓰기에 실패했습니다.')"); script.println("history.back()"); script.println("</script>"); } else { // 글쓰기에 성공했을 경우 PrintWriter script = response.getWriter(); script.println("<script>"); script.println("location.href= 'bbs.jsp'"); script.println("</script>"); } } } %> </body> </html> | cs |