1. 게시글 등록 페이지
- controller.board.Register.java
package controller.board;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import domain.Board;
import service.BoardService;
@WebServlet("/board/register")
public class Register extends HttpServlet {
private BoardService boardService = BoardService.getInstance();
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/jsp/board/register.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String title = req.getParameter("title");
String content = req.getParameter("content");
String writer = req.getParameter("writer");
Board board = new Board(null, title, content, writer);
boardService.register(board);
resp.sendRedirect("list");
}
}
- service.BoardService.java
// 글 작성
public void register(Board board) {
boardDao.register(board);
}
- dao.BoardDao.java
public void register(Board board) {
try {
Connection conn = DBConn.getConnection();
// 문장 생성
String sql = "INSERT INTO TBL_BOARD(BNO, TITLE, CONTENT, WRITER) "
+ "VALUES (SEQ_BOARD.NEXTVAL, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 파라미터 바인딩
pstmt.setString(1, board.getTitle());
pstmt.setString(2, board.getContent());
pstmt.setString(3, board.getWriter());
// 문장 실행(반영)
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<%@ include file="../common/head.jsp"%>
</head>
<body class="sb-nav-fixed">
<%@ include file="../common/nav.jsp"%>
<main class="mt-5 pt-5">
<div class="container-fluid px-4">
<h1 class="mt-4">Board</h1>
<div class="card mb-4">
<div class="card-body">
<form method="post">
<div class="mb-3 mt-3">
<label for="title" class="form-label">title</label> <input
type="text" class="form-control" id="title" name="title"
value="${board.title}">
</div>
<div class="mb-3">
<label for="content" class="form-label">content</label>
<textarea class="form-control" id="content" name="content">${board.content}</textarea>
</div>
<div class="mb-3">
<label for="writer" class="form-label">writer</label> <input
type="text" class="form-control" id="writer" name="writer"
value="${board.writer}">
</div>
<a href="list" class="btn btn-outline-secondary">list</a>
<button class="btn btn-outline-primary">register</button>
</form>
</div>
</div>
</div>
</main>
<%@ include file="../common/footer.jsp"%>
</body>
</html>
2. 게시글 삭제 페이지
- controller.board.Remove.java
package controller.board;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.BoardService;
@WebServlet("/board/remove")
public class Remove extends HttpServlet {
private BoardService boardService = BoardService.getInstance();
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long bno = Long.parseLong(req.getParameter("bno"));
boardService.remove(bno);
resp.sendRedirect("list");
}
}
- service.BoardService.java
// 글 삭제
public void remove(Long bno) {
boardDao.remove(bno);
}
- dao.BoardDao.java
public void remove(Long bno) {
try {
Connection conn = DBConn.getConnection();
// 문장 생성
String sql = "DELETE TBL_BOARD\r\n" +
"WHERE BNO = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 파라미터 바인딩
pstmt.setLong(1, bno);
// 문장 실행(반영)
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2. 회원가입 페이지
- controller.member.Join.java
package controller.member;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import domain.Member;
import service.MemberService;
@WebServlet("/member/join")
public class Join extends HttpServlet{
private MemberService memberService = MemberService.getInstance();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/jsp/member/join.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
String pw = req.getParameter("pw");
String name = req.getParameter("name");
Member member = new Member(id, pw, name);
memberService.register(member);
resp.sendRedirect(req.getContextPath() + "/board/list");
}
}
- service.MemberService.java
package service;
import dao.MemberDao;
import domain.Member;
public class MemberService {
private static MemberService memberService = new MemberService();
public static MemberService getInstance() {
return memberService;
}
private MemberService() {}
private MemberDao memberDao = MemberDao.getInstance();
public void register(Member member) {
memberDao.register(member);
}
}
- dao.MemberDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import domain.Member;
import utils.DBConn;
public class MemberDao {
// 싱글턴 적용
private static MemberDao memberDao = new MemberDao();
public static MemberDao getInstance() {
return memberDao;
}
private MemberDao() {}
public void register(Member member) {
try {
//클래스 로드
Connection conn = DBConn.getConnection();
// 문장 생성
String sql = "INSERT INTO TBL_MEMBER VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 파라미터 바인딩
int idx = 1;
pstmt.setString(idx++, member.id);
pstmt.setString(idx++, member.pw);
pstmt.setString(idx++, member.name);
// 문장 실행(반영)
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- join.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<%@ include file="../common/head.jsp"%>
</head>
<body class="bg-primary">
<div id="layoutAuthentication">
<div id="layoutAuthentication_content">
<main>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-7">
<div class="card shadow-lg border-0 rounded-lg mt-5">
<div class="card-header">
<h3 class="text-center font-weight-light my-4">Create
Account</h3>
</div>
<div class="card-body">
<form method="post">
<div class="form-floating mb-3">
<input class="form-control" id="id" type="text" name="id"
placeholder="enter ID" /> <label for="id">ID</label>
</div>
<div class="row mb-3">
<div class="col-md-6">
<div class="form-floating mb-3 mb-md-0">
<input class="form-control" id="inputPassword" name="pw"
type="password" placeholder="Create a password" /> <label
for="inputPassword">Password</label>
</div>
</div>
<div class="col-md-6">
<div class="form-floating mb-3 mb-md-0">
<input class="form-control" id="inputPasswordConfirm"
name="pw2" type="password" placeholder="Confirm password" />
<label for="inputPasswordConfirm">Confirm Password</label>
</div>
</div>
</div>
<div class="form-floating mb-3">
<input class="form-control" name="name" type="text"
placeholder="enter name" /> <label for="name">name</label>
</div>
<div class="mt-4 mb-0">
<div class="d-grid">
<button class="btn btn-primary btn-block">Create
Account</button>
</div>
</div>
</form>
</div>
<div class="card-footer text-center py-3">
<div class="small">
<a href="login.html">Have an account? Go to login</a>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
<div id="layoutAuthentication_footer">
<footer class="py-4 bg-light mt-auto">
<div class="container-fluid px-4">
<div
class="d-flex align-items-center justify-content-between small">
<div class="text-muted">Copyright © Your Website 2021</div>
<div>
<a href="#">Privacy Policy</a> · <a href="#">Terms
& Conditions</a>
</div>
</div>
</div>
</footer>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
crossorigin="anonymous"></script>
<script src="js/scripts.js"></script>
</body>
</html>