반응형
1. 게시글을 작성하는 form 및 화면 구현
write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter" %>
<!DOCTYPE html>
<html>
<head>
<meta 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-inverse">
<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="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: #2e8b57; 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-success pull-right" value="글쓰기">
</form>
</div>
</div>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
2. 게시글을 업데이트할 수 있도록 기능을 구현해야한다.
DdaDAO.java
package dda;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DdaDAO {
private Connection conn;
private ResultSet rs;
public DdaDAO() {
try {
String dbURL = "jdbc:mysql://localhost:3306/BBS?serverTimezone=UTC";
String dbID="root";
String dbPasseord="1248";
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(dbURL,dbID,dbPasseord);
}catch(Exception e) {
e.printStackTrace();
}
}
public String getDate() {
String SQL="select now()";
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 ddaID from DDA order by ddaID 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 ddaTitle, String userID, String ddaContent) {
String SQL="insert into BBS VALUES (?, ?, ?, ?, ?, ?)";
try {
PreparedStatement pstmt=conn.prepareStatement(SQL);
pstmt.setInt(1, getNext());
pstmt.setString(2, ddaTitle);
pstmt.setString(3, userID);
pstmt.setString(4, getDate());
pstmt.setString(5, ddaContent);
pstmt.setInt(6, 1);
return pstmt.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
}
return -1;
}
}
3. 글쓰기 버튼을 클릭하면 글이 저장될 수 있도록하는 페이지를 구현해야한다.
writeAction.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="dda.DdaDAO" %>
<%@ page import="java.io.PrintWriter" %>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="dda" class="dda.Dda" scope="page"/>
<jsp:setProperty name="dda" property="ddaTitle"/>
<jsp:setProperty name="dda" property="ddaContent" />
<!DOCTYPE html>
<html>
<head>
<meta 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(dda.getDdaTitle()==null||dda.getDdaContent()==null){
PrintWriter script=response.getWriter();
script.println("<script>");
script.println("alert('입력이 안 된 사항이 있습니다.')");
script.println("history.back()");
script.println("</script>");
}else{
DdaDAO ddaDAO=new DdaDAO();
int result=ddaDAO.write(dda.getDdaTitle(),userID,dda.getDdaContent());
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='dda.jsp'");
script.println("</script>");
}
}
}
%>
</body>
</html>
반응형
'CodeSiri > Project' 카테고리의 다른 글
[Mini Project 🚴🏻♀️] 11. 게시글 보기 기능 구현 (0) | 2021.02.22 |
---|---|
[Mini Project 🚴🏻♀️] 10. 게시판 글 목록 기능 구현 (0) | 2021.02.22 |
[Mini Project 🚴🏻♀️] 08. 게시판 데이터베이스 구축하기 (0) | 2021.02.22 |
[Mini Project 🚴🏻♀️] 07. 게시판 메인 페이지 디자인 (0) | 2021.02.22 |
[Mini Project 🚴🏻♀️] 06. 접속한 회원 세션 관리 (0) | 2021.02.22 |