BOARD.sql
xCREATE TABLE BOARD(
SEQ int PRIMARY KEY,
TITLE VARCHAR(200),
WRITER VARCHAR(20),
CONTENT VARCHAR(2000),
REGDATE datetime DEFAULT current_timestamp,
CNT int DEFAULT 0
);
INSERT INTO BOARD(SEQ, TITLE, WRITER, CONTENT) VALUES(1,'title1','writer1','content1');
INSERT INTO BOARD(SEQ, TITLE, WRITER, CONTENT) VALUES(2,'title2','writer2','content2');
SELECT * FROM BOARD ORDER BY SEQ;
web.xml
xxxxxxxxxx
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<description></description>
<display-name>DispatcherServlet</display-name>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>com.mvcboard.controller.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
xxxxxxxxxx
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
process(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
request.setCharacterEncoding("UTF-8"); // post 요청시 한글 처리
process(request, response);
}
xxxxxxxxxx
protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request path 추출
String uri = request.getRequestURI();
String path = uri.substring(uri.lastIndexOf("/"));
//System.out.println(path);
}
BoardController.java
xpackage com.mvcboard.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("*.do")
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DispatcherServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
process(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
request.setCharacterEncoding("UTF-8"); // post 요청시 한글 처리
process(request, response);
}
protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request path 추출
String uri = request.getRequestURI();
String path = uri.substring(uri.lastIndexOf("/"));
//System.out.println(path);
// request 처리
if(path.equals("/getBoardList.do")) {
System.out.println("/getBoardList.do");
}
}
}
BoardVO.java
xxxxxxxxxx
import java.sql.Date;
public class BoardVO {
private int seq;
private String title;
private String writer;
private String content;
private Date regDate;
private int cnt;
public BoardVO() {
}
public BoardVO(int seq, String title, String writer, String content, Date regDate, int cnt) {
super();
this.seq = seq;
this.title = title;
this.writer = writer;
this.content = content;
this.regDate = regDate;
this.cnt = cnt;
}
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "BoardVO [seq=" + seq + ", title=" + title + ", writer=" + writer + ", content"+ content + ", regdate=" + regDate + ", cnt=" + cnt +"]";
}
}
MySql
xxxxxxxxxx
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
BoardDAO.java
xxxxxxxxxx
package com.spring.web.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.spring.web.common.JDBCUtil;
import com.spring.web.vo.BoardVO;
public class BoardDAO{
// JDBC
private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet rs = null;
// SQL
private final String BOARD_INSERT = "insert into board(seq, title, writer,content) values((select COALESCE(max(seq),0)+1 from board a),?,?,?)";
private final String BOARD_UPDATE = "update board set title=?, content=? where seq=?";
private final String BOARD_DELETE = "delete board where seq=?";
private final String BOARD_GET = "select * from board where seq=?";
private final String BOARD_LIST = "select * from board order by seq desc";
// Method
// BOARD_INSERT
public void insertBoard(BoardVO vo) {
System.out.println("insertBoard()");
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_INSERT);
stmt.setString(1, vo.getTitle());
stmt.setString(2, vo.getWriter());
stmt.setString(3, vo.getContent());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
}
// BOARD_UPDATE
public void updateBoard(BoardVO vo) {
System.out.println("updatetBoard()");
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_UPDATE);
stmt.setString(1, vo.getTitle());
stmt.setString(2, vo.getContent());
stmt.setInt(3, vo.getSeq());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
}
// BOARD_DELETE
public void deleteBoard(BoardVO vo) {
System.out.println("deleteBoard()");
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_DELETE);
stmt.setInt(1, vo.getSeq());
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(stmt, conn);
}
}
// BOARD_GET
public BoardVO getBoard(BoardVO vo) {
System.out.println("getBoard()");
BoardVO board = null;;
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_GET);
stmt.setInt(1, vo.getSeq());
rs = stmt.executeQuery();
if(rs.next()) {
int seq = rs.getInt("seq");
String title = rs.getString("title");
String writer = rs.getString("writer");
String content = rs.getString("content");
Date regDate = rs.getDate("regdate");
int cnt = rs.getInt("cnt");
board = new BoardVO(seq, title, writer, content, regDate, cnt);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(rs, stmt, conn);
}
return board;
}
// BOARD_GET
public List<BoardVO> getBoardList() {
System.out.println("getBoardList()");
List<BoardVO> boardList = new ArrayList<BoardVO>();
try {
conn = JDBCUtil.getConnection();
stmt = conn.prepareStatement(BOARD_LIST);
rs = stmt.executeQuery();
while(rs.next()) {
int seq = rs.getInt("seq");
String title = rs.getString("title");
String writer = rs.getString("writer");
String content = rs.getString("content");
Date regDate = rs.getDate("regdate");
int cnt = rs.getInt("cnt");
BoardVO board = new BoardVO(seq, title, writer, content, regDate, cnt);
boardList.add(board);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(rs, stmt, conn);
}
return boardList;
}
}
BoardController.java
xxxxxxxxxx
if(path.equals("/getBoardList.do")) {
System.out.println("/getBoardList.do");
BoardVO vo = new BoardVO();
BoardDAO boardDAO = new BoardDAO();
List<BoardVO> boardList = boardDAO.getBoardList();
HttpSession httpSession = request.getSession();
httpSession.setAttribute("boardList", boardList);
response.sendRedirect("getBoardList.jsp");
}
getBoardList.jsp
xxxxxxxxxx
<%@page import="com.mvcboard.vo.BoardVO"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
List<BoardVO> boardList = (List)session.getAttribute("boardList");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>getBoardList.jsp</title>
<style>
th {width:100px; background-color: rgb(200, 150, 200); align:center;}
.title { width:300px;}
</style>
</head>
<body>
<center>
<h1>Board List</h1>
<h3>Login: Test <a href="logout.do">[Log-out]</a></h3>
<hr>
<!-- Search -->
<form action="getBoardList.do" method="post">
<table border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
<td align="right">
<select name="searchType">
<option value="TITLE">Title</option>
<option value="CONTENT">Content</option>
</select>
<input type="text" name="searchKeyword" >
<input type="submit" value="Search" >
</td>
</table>
</form>
<!-- List -->
<table border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
<th>Seq</th>
<th class="title">Title</th>
<th>Writer</th>
<th>RegDate</th>
<th>CNT</th>
</tr>
<% for(BoardVO board : boardList) {%>
<tr align="center">
<td><%=board.getSeq() %></th>
<td align="left"> <a href="getBoard.do?seq=<%=board.getSeq()%>"><%=board.getTitle() %></a></th>
<td><%=board.getWriter() %></th>
<td><%=board.getRegDate() %></th>
<td><%=board.getCnt() %></th>
</tr>
<%} %>
</table>
<hr>
<a href="insertBoard.jsp">[Board Write]</a>
<hr>
</center>
</body>
</html>
BoardController.java
xxxxxxxxxx
else if(path.equals("/getBoard.do")) {
System.out.println("/getBoard.do");
String seq = request.getParameter("seq");
BoardVO vo = new BoardVO();
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
BoardVO board = boardDAO.getBoard(vo);
HttpSession httpSession = request.getSession();
httpSession.setAttribute("board", board);
response.sendRedirect("getBoard.jsp");
}
getBoard.jsp
xxxxxxxxxx
<%@page import="com.mvcboard.vo.BoardVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
BoardVO board = (BoardVO)session.getAttribute("board");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>getBoard.jsp</title>
<style>
th {width:100px; background-color: rgb(200, 150, 200); align:center;}
</style>
</head>
<body>
<center>
<h1>Board View</h1>
<a href="logout.do">[Log-out]</a>
<hr>
<form action="updateBoard.do" method="post">
<input type="hidden" name="seq" value="<%=board.getSeq() %>">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th>Title</td>
<td> <input type="text" name="title" value="<%=board.getTitle() %>" size="40" /></td>
</tr>
<tr>
<th>Writer</td>
<td> <%=board.getWriter() %></td>
</tr>
<tr>
<th>Content</td>
<td><textarea name="content" cols="40" rows="10"><%=board.getContent() %></textarea></td>
</tr>
<tr>
<th>RegDate</td>
<td> <%=board.getRegDate() %></td>
</tr>
<tr>
<th>Count</td>
<td> <%=board.getCnt() %></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Board Modify" /></td>
</tr>
</table>
</form>
<hr>
<a href="insertBoard.jsp">[Board Write]</a>
<a href="deleteBoard.do?seq=<%=board.getSeq()%>">[Board Delete]</a>
<a href="getBoardList.do">[Board List]</a>
<hr>
</center>
</body>
</html>
insertBoard.jsp
xxxxxxxxxx
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>insertBoard.jsp</title>
<style>
th {width:100px; background-color: rgb(200, 150, 200); align:center;}
</style>
</head>
<body>
<center>
<h1>Board Write</h1>
<a href="logout.do">[Log-out]</a>
<hr>
<form action="insertBoard.do" method="post">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th>Title</td>
<td><input type="text" name="title" size="40" /></td>
</tr>
<tr>
<th>Writer</td>
<td><input type="text" name="writer" size="20" /></td>
</tr>
<tr>
<th>Content</td>
<td><textarea name="content" cols="40" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Board Write" /></td>
</tr>
</table>
</form>
<hr>
<a href="getBoardList.do">[Board List]</a>
<hr>
</center>
</body>
</html>
BoardController.java
xxxxxxxxxx
else if(path.equals("/insertBoard.do")) {
System.out.println("/insertBoard.do");
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String content = request.getParameter("content");
BoardVO vo = new BoardVO();
vo.setTitle(title);
vo.setWriter(writer);
vo.setContent(content);
BoardDAO boardDAO = new BoardDAO();
boardDAO.insertBoard(vo);
response.sendRedirect("getBoardList.do");
}
BoardController.java
xxxxxxxxxx
else if(path.equals("/updateBoard.do")) {
System.out.println("/updateBoard.do");
String title = request.getParameter("title");
String content = request.getParameter("content");
String seq = request.getParameter("seq");
BoardVO vo = new BoardVO();
vo.setTitle(title);
vo.setContent(content);
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
boardDAO.updateBoard(vo);
response.sendRedirect("getBoardList.do");
}
BoardController.java
xxxxxxxxxx
else if(path.equals("/deleteBoard.do")) {
System.out.println("/deleteBoard.do");
String seq = request.getParameter("seq");
BoardVO vo = new BoardVO();
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
boardDAO.deleteBoard(vo);
response.sendRedirect("getBoardList.do");
}
http://localhost:8080/MVCBoard/getBoardList.do
xpackage jspboard;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;
@WebServlet("*.do")
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DispatcherServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
process(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
request.setCharacterEncoding("UTF-8"); // post 요청시 한글 처리
process(request, response);
}
protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request path 추출
String uri = request.getRequestURI();
String path = uri.substring(uri.lastIndexOf("/"));
//System.out.println(path);
// Controller 호출 처리
BoardController controller = new BoardController();
controller.requestMapping(path, request, response);
}
}
xxxxxxxxxx
package jspboard;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class BoardController {
void requestMapping(String path, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
if(path.equals("/getBoardList.do")) {
System.out.println("/getBoardList.do");
BoardVO vo = new BoardVO();
BoardDAO boardDAO = new BoardDAO();
List<BoardVO> boardList = boardDAO.getBoardList();
request.setAttribute("boardList", boardList);
String view = "getBoardList.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
}else if(path.equals("/getBoard.do")) {
System.out.println("/getBoard.do");
String seq = request.getParameter("seq");
BoardVO vo = new BoardVO();
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
BoardVO board = boardDAO.getBoard(vo);
request.setAttribute("board", board);
String view = "getBoard.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
}else if(path.equals("/insertBoard.do")) {
System.out.println("/insertBoard.do");
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String content = request.getParameter("content");
BoardVO vo = new BoardVO();
vo.setTitle(title);
vo.setWriter(writer);
vo.setContent(content);
BoardDAO boardDAO = new BoardDAO();
boardDAO.insertBoard(vo);
response.sendRedirect("getBoardList.do");
}else if(path.equals("/updateBoard.do")) {
System.out.println("/updateBoard.do");
String title = request.getParameter("title");
String content = request.getParameter("content");
String seq = request.getParameter("seq");
BoardVO vo = new BoardVO();
vo.setTitle(title);
vo.setContent(content);
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
boardDAO.updateBoard(vo);
response.sendRedirect("getBoardList.do");
}else if(path.equals("/deleteBoard.do")) {
System.out.println("/deleteBoard.do");
String seq = request.getParameter("seq");
BoardVO vo = new BoardVO();
vo.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
boardDAO.deleteBoard(vo);
response.sendRedirect("getBoardList.do");
}
}
}