Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ★ bbs.jsp
- <%@ 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" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- <link rel="stylesheet" href="css/bootstrap.css">
- <style>
- a, a:hover {color:#000; text-decoration:none;}
- </style>
- </head>
- <body>
- <%
- 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-expanded="false">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="login.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+1)) {
- %>
- <a href="bbs.jsp?pageNumber=<%=pageNumber+1 %>" class="btn btn-success btn-arrow-left">다음</a>
- <%
- }
- %>
- <a href="write.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>
- ★ index.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <script>
- location.href = 'main.jsp';
- </script>
- </body>
- </html>
- ★join.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- <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="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="joinAction.jsp">
- <h3 style="text-align-center;">회원가입 화면</h3>
- <div class="form-group">
- <input type="text" class="form-control" placeholder="아이디" name="userId" maxlength="20">
- </div>
- <div class="form-group">
- <input type="password" class="form-control" placeholder="비밀번호" name="userPassword" maxlength="20">
- </div>
- <div class="form-group">
- <input type="text" class="form-control" placeholder="이름" name="userName" maxlength="20">
- </div>
- <div class="form-group" style="text-align:center;">
- <div class="btn-group" data-toggle="buttons">
- <label class="btn btn-primary active">
- <input type="radio" name="userGender" autocomplete="off" value="남자" checked>남자
- </label>
- <label class="btn btn-primary active">
- <input type="radio" name="userGender" autocomplete="off" value="여자" checked>여자
- </label>
- </div>
- </div>
- <div class="form-group">
- <input type="Email" class="form-control" placeholder="이메일" name="userEmail" maxlength="20">
- </div>
- <input type="submit" class="btn btn-primary form-control" value="회원가입">
- </form>
- </div>
- </div>
- </div>
- <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </body>
- </html>
- ★ joinAction.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" />
- <jsp:setProperty name="user" property="userName" />
- <jsp:setProperty name="user" property="userGender" />
- <jsp:setProperty name="user" property="userEmail" />
- <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("alert('비밀번호가 틀립니다.')");
- script.println("history.back()");
- script.println("</script>");
- }
- }
- %>
- </body>
- </html>
- ★ login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- <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="login.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="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="loginAction.jsp">
- <h3 style="text-align-center;">로그인 화면</h3>
- <div class="form-group">
- <input type="text" class="form-control" placeholder="아이디" name="userId" maxlength="20">
- </div>
- <div class="form-group">
- <input type="password" class="form-control" placeholder="비밀번호" name="userPassword" maxlength="20">
- </div>
- <input type="submit" class="btn btn-primary form-control" value="로그인">
- </form>
- </div>
- </div>
- </div>
- <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </body>
- </html>
- ★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" />
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>jsp 게시판 웹사이트</title>
- </head>
- <body>
- <%
- if(user.getUserId() ==null || user.getUserPassword() == null) {
- PrintWriter script = response.getWriter();
- script.println("<script>");
- script.println("alert('입력되지 않은 사항이 있습니다.')");
- script.println("history.back()");
- script.println("</script>");
- }
- String userId = null;
- if(session.getAttribute("userId") != null) {
- userId = (String) session.getAttribute("userId");
- }
- 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("alert('로그인 되었습니다. ')");
- 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 == -2 ) {
- PrintWriter script = response.getWriter();
- script.println("<script>");
- script.println("alert('데이터베이스 오류가 발생했습니다.')");
- script.println("history.back()");
- script.println("</script>");
- }
- %>
- </body>
- </html>
- ★ logoutAction.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- </head>
- <body>
- <%
- session.invalidate();
- %>
- <script>
- location.href = 'main.jsp';
- </script>
- </body>
- </html>
- ★ main.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import = "java.io.PrintWriter" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- <link rel="stylesheet" href="css/bootstrap.css">
- <link rel="stylesheet" href="css/custom.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="login.jsp">JSP게시판 웹 사이트</a>
- </div>
- <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
- <ul class="nav navbar-nav">
- <li class="active"><a href="main.jsp">메인</a></li>
- <li><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="jumbotron">
- <div class="container">
- <h1>해운대팬션</h1>
- <p>하늘소리바다소리</p>
- <a class="btn btn-primary btn-pull" href="#" role="button">자세히 알아보기</a>
- </div>
- </div>
- </div>
- <div class="container">
- <div id="myCorousel" class="carousel" data-ride="carousel">
- <ol class="carousel-indicators">
- <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
- <li data-target="#myCarousel" data-slide-to="1"></li>
- <li data-target="#myCarousel" data-slide-to="2"></li>
- </ol>
- <div class="carousel-inner">
- <div class="item active">
- <img src="images/b1.jpg">
- </div>
- <div class="item">
- <img src="images/b2.jpg">
- </div>
- <div class="item">
- <img src="images/b3.jpg">
- </div>
- </div>
- <a class="left carousel-control" href="#myCarousel" data-slide="prev">
- <span class="glyphicon-chevron-left"></span>
- </a>
- <a class="right carousel-control" href="#myCarousel" data-slide="next">
- <span class="glyphicon-chevron-right"></span>
- </a>
- </div>
- </div>
- <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
- <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" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- <link rel="stylesheet" href="css/bootstrap.css">
- </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;
- System.out.println(request.getParameter("bbsId"));
- 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-expanded="false">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="login.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>
- <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>
- ★ 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, java.lang.*" %>
- <% request.setCharacterEncoding("UTF-8"); %>
- <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("</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" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- <link rel="stylesheet" href="css/bootstrap.css">
- </head>
- <body>
- <%
- String userId = null;
- if(session.getAttribute("userId") != null ) {
- userId = (String) session.getAttribute("userId");
- }
- int bbsId = -1;
- if(request.getParameter("bbsId") != null ) {
- bbsId= Integer.parseInt(request.getParameter("bbsId"));
- }
- if(bbsId == -1) {
- 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-expanded="false">
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="login.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().replaceAll(" ", " ").replaceAll("<","<").replaceAll(" ",">")
- .replaceAll("\n","</br>") %></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(" ", " ")
- .replaceAll("<","<").replaceAll(" ",">").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>
- <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </body>
- </html>
- ★ write.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import = "java.io.PrintWriter" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 게시판 웹사이트</title>
- <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="login.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">
- <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>
- ★ Bbs.java
- package bbs;
- public class Bbs {
- private int bbsId;
- private String bbsTitle;
- private String userId;
- private String bbsDate;
- private String bbsContent;
- private int bbsAvailable;
- 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;
- }
- }
- ★ 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 con;
- private PreparedStatement pstmt;
- private ResultSet rs;
- public BbsDAO (){
- try {
- String dburl="jdbc:mysql://localhost:3306/shift";
- String dbId ="shift";
- String dbPassword = "shift";
- Class.forName("com.mysql.jdbc.Driver");
- con= DriverManager.getConnection(dburl, dbId, dbPassword);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String getDate (){
- String SQL = "SELECT NOW()";
- try {
- PreparedStatement pstmt = con.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 = con.prepareStatement(SQL);
- rs = pstmt.executeQuery();
- if(rs.next()) {
- return rs.getInt(1)+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 = con.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; // 데이터메이스 오류
- }
- public ArrayList <Bbs> getList (int pageNumber) {
- String SQL = "SELECT * FROM BBS WHERE bbsId < ? AND bbsAvailable = 1 ORDER BY bbsId DESC LIMIT 10";
- ArrayList <Bbs> list = new ArrayList<Bbs> ();
- try {
- PreparedStatement pstmt = con.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;
- }
- public boolean nextPage (int pageNumber) {
- String SQL = "SELECT * FROM BBS WHERE bbsId < ? AND bbsAvailable =1";
- try {
- PreparedStatement pstmt = con.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 bbs WHERE bbsId= ?";
- try {
- PreparedStatement pstmt = con.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 bbs set bbsTitle = ? , bbsContent = ? where bbsId= ? ";
- try {
- PreparedStatement pstmt = con.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 bbs set bbsAvailable = 0 where bbsId= ? ";
- try {
- PreparedStatement pstmt = con.prepareStatement(SQL);
- pstmt.setInt(1, bbsId);
- return pstmt.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return -1;
- }
- }
- ★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 con;
- private PreparedStatement pstmt;
- private ResultSet rs;
- public UserDAO (){
- try {
- String dburl="jdbc:mysql://localhost:3306/shift";
- String dbId ="shift";
- String dbPassword = "shift";
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(dburl, dbId, dbPassword);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public int login (String userId, String userPassword) {
- String SQL = "SELECT userPassword FROM USER WHERE userId=? " ;
- try {
- pstmt = con.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 USER VALUES (?, ?, ?, ?, ?)";
- try {
- pstmt = con.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;
- }
- }
- ★ writeAction.jsp
- <%@ 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"); %>
- <jsp:useBean id="bbs" class="bbs.Bbs" scope="page" />
- <jsp:setProperty name="bbs" property="bbsTitle" />
- <jsp:setProperty name="bbs" property="bbsContent" />
- <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>
- ★ 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"); %>
- <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("location.href= 'bbs.jsp' ");
- script.println("</script>");
- } else {
- PrintWriter script = response.getWriter();
- script.println("<script>");
- script.println("location.href= 'bbs.jsp' ");
- script.println("</script>");
- }
- }
- %>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement