[개발환경]

DB: Toad for SQL Server Freeware 5.8
eclipse: jee-oxygen
jdk: 1.8
tomcat: 9
bootstrap: 3.3.7(https://blog.naver.com/ndb796/221020960319)

인프런에서 나동빈님이 강의 중 [누구나 따라하면서 배우는 JSP 커뮤니티 게시판 만들기]를 수강해보았습니다. 제가 실습한 환경과 영상에서 만든 환경 구축 방법은 다릅니다. 해피님이 올려주신 코드도 참고하였습니다. 해피님 블로그로 가시면 수강하면서 도움 받을 수 있습니다.

  • 수강 후 느낀점
    부트스트랩이 뭔지 한번에 느낄 수 있습니다.
    JSP 기초에 대해서 알 수 있습니다.
    로그인에 대한 기본 개념을 학습 할 수 있습니다.
    DB에 대한 기본 개념을 학습 할 수 있습니다.

기록으로 남기기 위해 포스트 작성합니다.

login.jsp

<%@ 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">
<title>jsp 게시판 웹 사이트</title>
<meta name="viewport" content="width=device-width", initial-scale="1">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.js"></script>

<link rel="stylesheet" href="css/bootstrap.css">
</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-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
            <li><a href="main.jsp">메인</a></li>
            <li><a href="bbs.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="ture"
                    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="loginAction.jsp">
                <h3 style="text-align:center">로그인 화면</h3>
                <div class="form-group">
                    <input type ="text" class="form-control" placeholder="아이디" name="userID" mexlength="20">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" placeholder="비밀번호" name="userPassword" mexlength="20">
                </div>
                <input type="submit" class="btn btn-primary form-control"  value="로그인">
            </form>        
        </div>
    </div>
</div>


</body>
</html>

User.java

package user;

public class User {

    private String userID;
    private String userPassword;
    private String userName;
    private String userGender;
    private String userEmail;

    public String getUserID() {
        return userID;
    }
    public void setUserID(String userID) {
        this.userID = userID;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserGender() {
        return userGender;
    }
    public void setUserGender(String userGender) {
        this.userGender = userGender;
    }
    public String getUserEmail() {
        return userEmail;
    }
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }


}

UserDAO.java

package user;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAO {

    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    public UserDAO() {
        try {

            String dbURL= "jdbc:sqlserver://0.0.0.0;databaseName=BBS";
            String dbID="#";
            String dbPassword="#";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(dbURL, dbID, dbPassword);

        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public int login(String userID, String userPassword) {
        //System.Println("1111111111111111");
        String SQL = "SELECT userPassword FROM dbo.[USER] WHERE userID =  ?";

        try {
            pstmt = conn.prepareStatement(SQL);
            pstmt.setString(1, userID);
            rs = pstmt.executeQuery();
            if(rs.next()){
                if(rs.getString(1).equals(userPassword))
                    return 1; // 로그인 성공
                else
                    return 0; // 비밀번호 불일치
            }
            return -1; // 아이디가 없음
        }catch(Exception e) {
            e.printStackTrace();
        }
        return -2; // 데이터베이스 오류 
    }

    public int join(User user) {
         String SQL = "insert into dbo.[USER](userID, userPassword, userName, userGender, userEmail) values(?,?,?,?,?)";
         //String SQL = "INSERT INTO USER VALUES (?,?,?,?,?)";
         //insert into dbo.[USER](userID, userPassoword, userName, userGender, userEmail)values('gildong', '123456', '홍길동', '남자', 'gildong@naver.com');
         //SELECT userPassword FROM dbo.[USER] WHERE userID =  'sdfsdf';
         try {

             pstmt = conn.prepareStatement(SQL);

              pstmt.setString(1,  user.getUserID());
              pstmt.setString(2,  user.getUserPassword());
              pstmt.setString(3,  user.getUserName());
              pstmt.setString(4,  user.getUserGender());
              pstmt.setString(5,  user.getUserEmail());

              return pstmt.executeUpdate();

         } catch(Exception e) {
             e.printStackTrace();
         }
         return -1; // 데이터 베이스 오류 
    }

}

loginAction.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="user.UserDAO" %>
<%@ page import="java.io.PrintWriter" %>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="user" class="user.User" scope="page"/>
<jsp:setProperty name="user" property="userID"/>
<jsp:setProperty name="user" property="userPassword"/>

<!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("history.back()");
        script.println("</script>");
    }

    UserDAO userDAO = new UserDAO();

    int result= userDAO.login(user.getUserID(), user.getUserPassword());

    if(result == 1){
        session.setAttribute("userID", user.getUserID());
        PrintWriter script = response.getWriter();
        script.println("<script>");
        script.println("location.href = 'main.jsp'");
        script.println("</script>");
    }else if(result == 0){
        PrintWriter script = response.getWriter();
        script.println("<script>");
        script.println("alert('비밀번호가 틀립니다.');");
        script.println("history.back()");
        script.println("</script>");
    }else if(result == -1){
        PrintWriter script = response.getWriter();
        script.println("<script>");
        script.println("alert('존재하지 않는 아이디 입니다.');");
        script.println("history.back()");
        script.println("</script>");
    }else if(result == -1){
        PrintWriter script = response.getWriter();
        script.println("<script>");
        script.println("alert('데이터베이스 오류가 발생했습니다.');");
        script.println("history.back()");
        script.println("</script>");
    }

%>
</body>
</html>

join.jsp

<%@ 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">
<title>jsp 게시판 웹 사이트</title>
<meta name="viewport" content="width=device-width", initial-scale="1">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.js"></script>

<link rel="stylesheet" href="css/bootstrap.css">
</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-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
            <li><a href="main.jsp">메인</a></li>
            <li><a href="bbs.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="ture"
                    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="loginAction.jsp">
                <h3 style="text-align:center">로그인 화면</h3>
                <div class="form-group">
                    <input type ="text" class="form-control" placeholder="아이디" name="userID" mexlength="20">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" placeholder="비밀번호" name="userPassword" mexlength="20">
                </div>
                <input type="submit" class="btn btn-primary form-control"  value="로그인">
            </form>        
        </div>
    </div>
</div>


</body>
</html>

UserDAO.java 에 join 추가

joinAcrion.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="user.UserDAO" %> <!-- userdao의 클래스 가져옴 -->
<%@ page import="java.io.PrintWriter" %> <!-- 자바 클래스 사용 -->
<% request.setCharacterEncoding("UTF-8"); %>
<!-- 한명의 회원정보를 담는 user클래스를 자바 빈즈로 사용 / scope:페이지 현재의 페이지에서만 사용-->
<jsp:useBean id="user" class="user.User" scope="page" />
<jsp:setProperty name="user" property="userID" />
<jsp:setProperty name="user" property="userPassword" /> 
<jsp:setProperty name="user" property="userName" />
<jsp:setProperty name="user" property="userGender" />
<jsp:setProperty name="user" property="userEmail" />

<!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 = 'main.jsp'");
                script.println("</script>");    
            }


        if (user.getUserID() == null || user.getUserPassword() == null || user.getUserName() == null
            || user.getUserGender() == null || user.getUserEmail() == null){
                PrintWriter script = response.getWriter();
                script.println("<script>");
                script.println("alert('입력이 안 된 사항이 있습니다.')");
                script.println("history.back()");
                script.println("</script>");
            } else{
                UserDAO userDAO = new UserDAO(); //인스턴스생성
                int result = userDAO.join(user);                

                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 = 'main.jsp'");
                    script.println("</script>");
                }
            }
            %>

</body>
</body>
</html>

logoutAction.jsp


<%@ 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">
<title>jsp 게시판 웹 사이트</title>
</head>
<body>
    <%
        session.invalidate();
    %>
    <script>
        location.href = 'main.jsp';
    </script>


</body>
</html>

bbs.jsp

    <%@page import="javax.security.auth.callback.ConfirmationCallback"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.io.PrintWriter"%>
    <%@ page import="bbs.BbsDAO"%>
    <%@ page import="bbs.Bbs"%>
    <%@ page import="java.util.ArrayList"%>
    <!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>
    <style type="text/css">
        a, a:hover {
            color: #000000;
            text-decoration: none;
        }
    </style>
    </head>
    <body>
        <%
            //로긴한사람이라면     userID라는 변수에 해당 아이디가 담기고 그렇지 않으면 null값
            String userID = null;
            if (session.getAttribute("userID") != null) {
                userID = (String) session.getAttribute("userID");

            }

            int pageNumber = 1; //기본 페이지 넘버

            //페이지넘버값이 있을때
            if (request.getParameter("pageNumber") != null) {
                pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
            }
        %>


        <!-- 네비게이션  -->
        <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-expaned="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="logoutAction.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="background-color: #eeeeee; text-align: center;">번호</th>
                            <th style="background-color: #eeeeee; text-align: center;">제목</th>
                            <th style="background-color: #eeeeee; text-align: center;">작성자</th>
                            <th style="background-color: #eeeeee; text-align: center;">작성일</th>
                        </tr>
                    </thead>
                    <tbody>
                        <%
                            BbsDAO bbsDAO = new BbsDAO();
                            ArrayList<Bbs> list = bbsDAO.getList(pageNumber);
                            for (int i = 0; i < list.size(); i++) {
                        %>
                        <tr>
                            <td><%=list.get(i).getBbsID()%></td>
                            <td><a href="view.jsp?bbsID=<%=list.get(i).getBbsID()%>"><%=list.get(i).getBbsTitle()%></a></td>
                            <td><%=list.get(i).getUserID()%></td>
                            <td><%=list.get(i).getBbsDate().substring(0, 11) + list.get(i).getBbsDate().substring(11, 13) + "시"
                            + list.get(i).getBbsDate().substring(14, 16) + "분"%></td>
                        </tr>

                        <%
                            }
                        %>

                    </tbody>
                </table>
                <!-- 페이지 넘기기 -->
                <%
                    if (pageNumber != 1) {
                %>
                <a href="bbs.jsp?pageNumber=<%=pageNumber - 1%>"
                    class="btn btn-success btn-arrow-left">이전</a>
                <%
                    }
                    if (bbsDAO.nextPage(pageNumber)) {
                %>
                <a href="bbs.jsp?pageNumber=<%=pageNumber + 1%>"
                    class="btn btn-success btn-arrow-left">다음</a>
                <%
                    }
                %>


                <!-- 회원만넘어가도록 -->
                <%
                    //if logined userID라는 변수에 해당 아이디가 담기고 if not null
                    if (session.getAttribute("userID") != null) {
                %>
                <a href="write.jsp" class="btn btn-primary pull-right">글쓰기</a>
                <%
                    } else {
                %>
                <button class="btn btn-primary pull-right"
                    onclick="if(confirm('로그인 하세요'))location.href='login.jsp';"
                    type="button">글쓰기</button>
                <%
                    }
                %>

            </div>
        </div>

        <!-- 애니매이션 담당 JQUERY -->
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <!-- 부트스트랩 JS  -->
        <script src="js/bootstrap.js"></script>

    </body>
    </html>

Bbs.java

package bbs;

public class Bbs {
    public int getBbsID() {
        return bbsID;
    }
    public void setBbsID(int bbsID) {
        this.bbsID = bbsID;
    }
    public String getBbsTitle() {
        return bbsTitle;
    }
    public void setBbsTitle(String bbsTitle) {
        this.bbsTitle = bbsTitle;
    }
    public String getUserID() {
        return userID;
    }
    public void setUserID(String userID) {
        this.userID = userID;
    }
    public String getBbsDate() {
        return bbsDate;
    }
    public void setBbsDate(String bbsDate) {
        this.bbsDate = bbsDate;
    }
    public String getBbsContent() {
        return bbsContent;
    }
    public void setBbsContent(String bbsContent) {
        this.bbsContent = bbsContent;
    }
    public int getBbsAvailable() {
        return bbsAvailable;
    }
    public void setBbsAvailable(int bbsAvailable) {
        this.bbsAvailable = bbsAvailable;
    }
    private int bbsID;
    private String bbsTitle;
    private String userID;
    private String bbsDate;
    private String bbsContent;
    private int bbsAvailable;

}

write.jsp

<%@ 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">
<title>jsp 게시판 웹 사이트</title>
<meta name="viewport" content="width=device-width", initial-scale="1">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.js"></script>

<link rel="stylesheet" href="css/bootstrap.css">
</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-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-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="ture"
                    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="ture"
                    aria-expanded="false">나의정보<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="logoutAction.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: #eee; 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 type="text" 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>

</body>
</html>

BbsDAO.java

package bbs;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class BbsDAO {

    private Connection conn;
    private ResultSet rs;

    public BbsDAO() {
        try {

            String dbURL= "jdbc:sqlserver://0.0.0.0;databaseName=BBS";
            String dbID="#";
            String dbPassword="#";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(dbURL, dbID, dbPassword);

        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public String getDate() {
        String SQL = "Select GETDATE();";
        //String SQL = "Select NOW()";
        //Select GETDATE();
        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 dbo.[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 VALUE (?,?,?,?,?)";
        String SQL = "insert into dbo.[BBS](bbsID, bbsTitle, userID, bbsDate, bbsContent, bbsAvailable) 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);
            //rs = pstmt.executeQuery();
            //System.out.println(SQL);
            return pstmt.executeUpdate();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return -1; // 데이터베이스 오류
    }

    public ArrayList<Bbs> getList(int pageNumber){ 
        //String SQL = "SELECT * FROM BBS WHERE bbsID < ? bbsAvailable = 1 ORDER BY bbsID DESC LIMIT 10";
        String SQL = "SELECT * FROM  dbo.[BBS] WHERE 1=1 AND bbsID < ? AND bbsAvailable = 1 ORDER BY bbsID DESC OFFSET  0 ROWS FETCH NEXT 10 ROWS ONLY";
        //SELECT * FROM  dbo.[BBS] WHERE 1=1 AND bbsID < 10 AND bbsAvailable = 1 ORDER BY bbsID DESC OFFSET  1 ROWS FETCH NEXT 10 ROWS ONLY;
        ArrayList<Bbs> list = new ArrayList<Bbs>();
        try {
            PreparedStatement pstmt = conn.prepareStatement(SQL);
            pstmt.setInt(1, getNext() - (pageNumber -1) * 10);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Bbs bbs = new Bbs();
                bbs.setBbsID(rs.getInt(1));
                bbs.setBbsTitle(rs.getString(2));
                bbs.setUserID(rs.getString(3));
                bbs.setBbsDate(rs.getString(4));
                bbs.setBbsContent(rs.getString(5));
                bbs.setBbsAvailable(rs.getInt(6));
                list.add(bbs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list; 
    }

    //10 단위 페이징 처리를 위한 함수
    public boolean nextPage (int pageNumber) {
        //String SQL = "SELECT * FROM BBS WHERE bbsID < ? bbsAvailable = 1 ORDER BY bbsID DESC LIMIT 10";
        String SQL = "SELECT * FROM  dbo.[BBS] WHERE 1=1 AND bbsID < ? AND bbsAvailable = 1 ORDER BY bbsID DESC OFFSET  0 ROWS FETCH NEXT 10 ROWS ONLY";
        ArrayList<Bbs> list = new ArrayList<Bbs>();
        try {
            PreparedStatement pstmt = conn.prepareStatement(SQL);
            pstmt.setInt(1, getNext() - (pageNumber -1) * 10);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;         
    }

    public Bbs getBbs(int bbsID) {
        String SQL = "SELECT * FROM dbo.[BBS] WHERE bbsID = ?";
        //String SQL = "SELECT * FROM BBS WHERE bbsID = ?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(SQL);
            pstmt.setInt(1, bbsID);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Bbs bbs = new Bbs();
                bbs.setBbsID(rs.getInt(1));
                bbs.setBbsTitle(rs.getString(2));
                bbs.setUserID(rs.getString(3));
                bbs.setBbsDate(rs.getString(4));
                bbs.setBbsContent(rs.getString(5));
                bbs.setBbsAvailable(rs.getInt(6));

                return bbs;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    //수정 함수
    public int update(int bbsID, String bbsTitle, String bbsContent) {
            String SQL = "UPDATE dbo.[BBS] SET bbsTitle = ?, bbsContent = ? WHERE bbsID = ?";
            try {
                PreparedStatement pstmt = conn.prepareStatement(SQL);
                pstmt.setString(1, bbsTitle);
                pstmt.setString(2, bbsContent);
                pstmt.setInt(3, bbsID);
                return pstmt.executeUpdate();

            } catch (Exception e) {
                e.printStackTrace();
            }
            return -1; // 데이터베이스 오류
    }


    //삭제 함수
    public int delete(int bbsID) {
        String SQL = "UPDATE dbo.[BBS] SET bbsAvailable = 0 WHERE bbsID = ?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(SQL);   
            pstmt.setInt(1, bbsID);
            return pstmt.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1; // 데이터베이스 오류
    }


}

writeAction.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="bbs.BbsDAO"%>
<!-- bbsdao의 클래스 가져옴 -->
<%@ page import="java.io.PrintWriter"%>
<!-- 자바 클래스 사용 -->
<%
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8"); //set으로쓰는습관들이세오.
%>

<!-- 한명의 회원정보를 담는 user클래스를 자바 빈즈로 사용 / scope:페이지 현재의 페이지에서만 사용-->
<jsp:useBean id="bbs" class="bbs.Bbs" scope="page" />
<!-- // Bbs bbs = new Bbs(); -->
<jsp:setProperty name="bbs" property="bbsTitle" /><!-- bbs.setBbsTitle(requrst) -->
<jsp:setProperty name="bbs" property="bbsContent" />
<%
    System.out.println(bbs);
%>
<!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>

view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="bbs.Bbs"%>
<%@ page import="bbs.BbsDAO"%>
<!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>
    <%
        //로긴한사람이라면     userID라는 변수에 해당 아이디가 담기고 그렇지 않으면 null값
        String userID = null;
        if (session.getAttribute("userID") != null) {
            userID = (String) session.getAttribute("userID");

        }
        int bbsID = 0;
        if (request.getParameter("bbsID") != null) {
            bbsID = Integer.parseInt(request.getParameter("bbsID"));
        }
        if (bbsID == 0) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('유효하지 않은 글 입니다.')");
            script.println("location.href = 'bbs.jsp'");
            script.println("</script>");
        }
        Bbs bbs = new BbsDAO().getBbs(bbsID);
    %>


    <!-- 네비게이션  -->
    <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-expaned="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="logoutAction.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 colspan="3"
                                style="background-color: #eeeeee; text-align: center;">글 보기 </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td style="width: 20%;"> 글 제목 </td>
                            <td colspan="2"><%= bbs.getBbsTitle() %></td>
                        </tr>
                        <tr>
                            <td>작성자</td>    
                            <td colspan="2"><%= bbs.getUserID() %></td>
                        </tr>
                        <tr>
                            <td>작성일</td>    
                            <td colspan="2"><%= bbs.getBbsDate().substring(0, 11) + bbs.getBbsDate().substring(11, 13) + "시"
                            + bbs.getBbsDate().substring(14, 16) + "분"%></td>
                        </tr>
                        <tr>
                            <td>내용</td>    
                            <td colspan="2" style="min-height: 200px; text-align: left;"><%= bbs.getBbsContent().replaceAll(" ", "&nbsp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br/>") %>
                            </td>
                        </tr>

                    </tbody>
                </table>    
                <a href = "bbs.jsp" class="btn btn-primary">목록</a>


                <%
                //글작성자 본인일시 수정 삭제 가능 
                    if(userID != null && userID.equals(bbs.getUserID())){
                %>
                    <a href="update.jsp?bbsID=<%= bbsID %>" class="btn btn-primary">수정</a>
                    <a onclick="return confirm('정말로 삭제하시겠습니까?')" href="deleteAction.jsp?bbsID=<%= bbsID %>" class="btn btn-primary    ">삭제</a>
                <%                    
                    }
                %>
        </div>
    </div>




    <!-- 애니매이션 담당 JQUERY -->
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <!-- 부트스트랩 JS  -->
    <script src="js/bootstrap.js"></script>

</body>
</html>

update.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="bbs.Bbs"%>
<%@ page import="bbs.BbsDAO"%>
<!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>
    <%
        //로긴한사람이라면     userID라는 변수에 해당 아이디가 담기고 그렇지 않으면 null값
        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>");

        }

        int bbsID = 0;
        if (request.getParameter("bbsID") != null) {
            bbsID = Integer.parseInt(request.getParameter("bbsID"));
        }
        if (bbsID == 0) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('유효하지 않은 글 입니다.')");
            script.println("location.href = 'bbs.jsp'");
            script.println("</script>");
        }
        Bbs bbs = new BbsDAO().getBbs(bbsID);
        if (!userID.equals(bbs.getUserID())) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('권한이 없습니다.')");
            script.println("location.href = 'bbs.jsp'");
            script.println("</script>");                
        }
    %>


    <!-- 네비게이션  -->
    <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-expaned="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>
            <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="logoutAction.jsp">로그아웃</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </nav>
    <!-- 게시판 --> 
    <div class="container">
        <div class="row">
            <form method="post" action="updateAction.jsp?bbsID=<%= bbsID %> ">
                <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" value="<%= bbs.getBbsTitle() %>" ></td>
                        </tr>
                        <tr>
                            <td><textarea class="form-control" placeholder="글 내용" name="bbsContent" maxlength="2048" style="height: 350px;" ><%= bbs.getBbsContent() %></textarea></td>
                        </tr>
                    </tbody>
                </table>    
                <button type="submit" class="btn btn-primary pull-right" >글수정</button>
            </form>
        </div>
    </div>




    <!-- 애니매이션 담당 JQUERY -->
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <!-- 부트스트랩 JS  -->
    <script src="js/bootstrap.js"></script>

</body>
</html>

updateAction.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="bbs.BbsDAO"%>
<%@ page import="bbs.Bbs"%>
<%@ page import="java.io.PrintWriter"%>
<%
    request.setCharacterEncoding("UTF-8");
    //sresponse.setContentType("text/html; charset=UTF-8");
%>
<!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>");
        } 

        //글이 유효한지 판별
        int bbsID = 0;
        if (request.getParameter("bbsID") != null) {
            bbsID = Integer.parseInt(request.getParameter("bbsID"));
        }
        if (bbsID == 0) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('유효하지 않은 글 입니다.')");
            script.println("location.href = 'bbs.jsp'");
            script.println("</script>");
        }
        Bbs bbs = new BbsDAO().getBbs(bbsID);
        if (!userID.equals(bbs.getUserID())) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('권한이 없습니다.')");
            script.println("location.href = 'bbs.jsp'");
            script.println("</script>");                
        } else {
            if (request.getParameter("bbsTitle") == null || request.getParameter("bbsContent") == null
                    || request.getParameter("bbsTitle").equals("") || request.getParameter("bbsContent").equals("") ) {
                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.update(bbsID, request.getParameter("bbsTitle"), request.getParameter("bbsContent"));
                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("history.back()");
                    script.println("</script>");
                }

            }

        }
    %>
</body>
</html>

deleteAction.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="bbs.BbsDAO"%>
<%@ page import="bbs.Bbs"%>
<%@ page import="java.io.PrintWriter"%>
<%
    request.setCharacterEncoding("UTF-8");
    //response.setContentType("text/html; charset=UTF-8");
%>
<!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>");
        } 
        int bbsID = 0;
        if(request.getParameter("bbsID") != null){
            bbsID = Integer.parseInt(request.getParameter("bbsID"));
        }
        if(bbsID == 0) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('유효하지 않은 글 입니다')");
            script.println("location.href='bbs.jsp'");
            script.println("</script>");
        }
        Bbs bbs = new BbsDAO().getBbs(bbsID);
        if(!userID.equals(bbs.getUserID())) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('권한이 없습니다')");
            script.println("location.href='bbs.jsp'");
            script.println("</script>");            
        }
        else{
            BbsDAO bbsDAO = new BbsDAO();
            int result = bbsDAO.delete(bbsID);
            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>