게시판 메인페이지 디자인
bbs.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">
<meta name="viewport" content="width=device-width", initial-scale="1" > <!-- 반응형 웹에 사용하는 메타태그 -->
<link rel="stylesheet" href="css/bootstrap.css"> <!-- 참조 -->
<title>JSP 게시판 웹 사이트</title>
</head>
<body>
<%
String userID = null; // 로그인이 된 사람들은 로그인정보를 담을 수 있도록한다
if (session.getAttribute("userID") != null)
{
userID = (String)session.getAttribute("userID");
}
%>
<nav class ="navbar navbar-default">
<div class="navbar-header"> <!-- 홈페이지의 로고 -->
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"
aria-expand="false">
<span class ="icon-bar"></span> <!-- 줄였을때 옆에 짝대기 -->
<span class ="icon-bar"></span>
<span class ="icon-bar"></span>
</button>
<a class ="navbar-brand" href="main.jsp">JSP 게시판 웹 사이트</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="main.jsp">메인</a></li>
<li class="active"><a href="bbs.jsp">게시판</a></li>
</ul>
<%
// 접속하기는 로그인이 되어있지 않은 경우만 나오게한다
if(userID == null)
{
%>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class = "dropdown-toggle"
data-toggle="dropdown" role ="button" aria-haspopup="true"
aria-expanded="false">접속하기<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="login.jsp">로그인</a></li>
<li><a href="join.jsp">회원가입</a></li>
</ul>
</li>
</ul>
<%
// 로그인이 되어있는 사람만 볼수 있는 화면
} else {
%>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class = "dropdown-toggle"
data-toggle="dropdown" role ="button" aria-haspopup="true"
aria-expanded="false">회원관리<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="loginAction.jsp">로그아웃</a></li>
</ul>
</li>
</ul>
<%
}
%>
</div>
</nav>
<div class="container">
<div class="row">
<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>
<tr>
<td>1</td>
<td>안녕하세요</td>
<td>홍길동</td>
<td>2017-05-04</td>
</tr>
</tbody>
</table>
<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>
게시판 데이터베이스 구축하기
테이블 만들기
use bbs;
CREATE TABLE BBS (
bbsID INT,
bbsTitle VARCHAR(50),
userID VARCHAR(20),
bbsDate DATETIME,
bbsContent VARCHAR(2048),
bbsAvailable INT,
PRIMARY KEY (bbsID)
);
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;
}
}
글쓰기 기능 구현하기
package bbs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class bbsDAO {
private Connection conn;
private ResultSet rs;
public bbsDAO() {
try {
String dbURL = "jdbc:mysql://localhost:3306/BBS";
String dbID = "root";
String dbPassword = "1234";
Class.forName("com.mysql.jdbc.Drvier");
conn = DriverManager.getConnection(dbURL, dbID, dbPassword);
} 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 bbsID FROM FROM BBS ORDER BY bbsID DESC";
try {
PreparedStatement pstmt = conn.prepareStatement(SQL);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt(1) + 1;
}
return 1; //첫번째 게시물인 경우
} catch (Exception e) {
e.printStackTrace();
}
return -1; //데이터베이스 오류
}
public int write(String bbsTitle, String userID, String bbsContent) {
String SQL = "INSERT INTO BBS VALUE (?, ?, ?, ?, ?, ?)";
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();
return pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return -1; //데이터베이스 오류
}
}
writeAction.jsp
<!-- 실제로 글쓰기를 눌러서 만들어주는 Action페이지 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="bbs.BbsDAO" %>
<%@ page import="java.io.PrintWriter" %> <!-- 자바스크립트 문장사용 -->
<% request.setCharacterEncoding("UTF-8"); %> <!-- 건너오는 모든 파일을 UTF-8로 -->
<jsp:useBean id="bbs" class="bbs.Bbs" scope="page"/>
<jsp:setProperty name="bbs" property="bbsTitle" />
<jsp:setProperty name="bbs" property="bbsContent" />
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP게시판 웹사이트</title>
</head>
<body>
<%
String userID = null;
// 로그인 된 사람은 회원가입페이지에 들어갈수 없다
if(session.getAttribute("userID") != null )
{
userID = (String) session.getAttribute("userID");
}
if(userID != null)
{
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('로그인을 하세요')");
script.println("location.href = 'login.jsp'");
script.println("</script>");
} else {
if(bbs.Bbs.getBbsTitle() == null || bbs.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.Bbs.getBbsTitle(), userID, bbs.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>
'Back-end > JAVA' 카테고리의 다른 글
Mybatis Mapper XML <select> 정리 (1) | 2022.09.08 |
---|---|
JAVA Annotation 정리 (0) | 2022.09.05 |
JSP 게시판 만들기 - 2 (0) | 2022.08.20 |
JSP 게시판 만들기 - 1 (0) | 2022.08.20 |
Spring Bean Scopes and Lifecycle 정리 (0) | 2020.11.25 |