위로 아래

전체 파일 구조

 

 

 

action 전체 코드

action.CommandAction

더보기
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface CommandAction {
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception;

}

 

action.ContentAction

더보기
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardDAO;
import model.BoardDTO;

public class ContentAction implements CommandAction {

	@Override
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		BoardDAO dao = BoardDAO.getInstance();
		BoardDTO article = new BoardDTO();
		
		int bno = Integer.parseInt(request.getParameter("bno"));
		
		article = dao.getArticle(bno);
		
		request.setAttribute("article", article);
		
		return "view/ContentView.jsp?bno="+bno;
	}

}

 

action.DeleteAction

더보기
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardDAO;

public class DeleteAction implements CommandAction {

	@Override
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		int bno = Integer.parseInt(request.getParameter("bno"));
		int r = 0;
		
		BoardDAO dao = BoardDAO.getInstance();
		
		r = dao.deleteArticle(bno);
		
		String msg = null;
		String page = "list.do";
		
		if(r>0) msg = "삭제 성공";
		else msg = "삭제 실패";
		
		request.setAttribute("msg", msg);
		request.setAttribute("page", page);
		
		return "view/MsgPage.jsp";
	}

}

 

action.ListAction

더보기
package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardDAO;
import model.BoardDTO;
import model.PageBean;

public class ListAction implements CommandAction {

	@Override
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
		BoardDAO dao = BoardDAO.getInstance();
		
		int currentPage = 1;
		int totalCnt = dao.getTotalCnt();
		
		// p가 이미 있을 때 글목록 버튼을 누르면 해당 페이지로 갈 수 있도록 currentPage에 ListView 페이지의 p값을 대입.
		// p가 없을 경우 위의 초기화에서 currentPage=1준 것을 쓴다.
		if(request.getParameter("p") != null) {
			currentPage = Integer.parseInt(request.getParameter("p"));
		}
		
		int start = (currentPage-1)*PageBean.LINE_OF_PAGE + 1;
		int end = (currentPage*PageBean.LINE_OF_PAGE >= totalCnt) ? totalCnt : currentPage*PageBean.LINE_OF_PAGE;
		
		int pageCnt=1;
		if(totalCnt%PageBean.LINE_OF_PAGE == 0) pageCnt = totalCnt/PageBean.LINE_OF_PAGE;
		else pageCnt = totalCnt/PageBean.LINE_OF_PAGE + 1;
		
		List<BoardDTO> articles = dao.getArticles(start,end);
		
		request.setAttribute("p", currentPage);
		request.setAttribute("totalCnt", totalCnt);
		request.setAttribute("articles", articles);
		request.setAttribute("pageCnt", pageCnt);
		
		return "view/ListView.jsp?p="+currentPage;
	}

}

 

action.UpdateAction

더보기
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardDTO;

public class UpdateAction implements CommandAction {

	@Override
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		BoardDTO dto = new BoardDTO();
		
		dto.setBno(Integer.parseInt(request.getParameter("bno")));
		dto.setBref(Integer.parseInt(request.getParameter("bref")));
		dto.setBstep(Integer.parseInt(request.getParameter("bstep")));
		dto.setBlevel(Integer.parseInt(request.getParameter("blevel")));
		dto.setSubject(request.getParameter("subject"));
		dto.setWriter(request.getParameter("writer"));
		dto.setContent(request.getParameter("content"));
		dto.setPasswd(request.getParameter("pw"));
		
		request.setAttribute("article", dto);
		
		return "view/UpdateView.jsp";
	}

}

 

action.UpdateProc

더보기
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardDAO;
import model.BoardDTO;

public class UpdateProc implements CommandAction {

	@Override
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
		BoardDTO dto = new BoardDTO();
		int result = 0;
		String msg = null;
		String page = null;
		
		dto.setBno(Integer.parseInt(request.getParameter("bno")));
		dto.setBref(Integer.parseInt(request.getParameter("bref")));
		dto.setBstep(Integer.parseInt(request.getParameter("bstep")));
		dto.setBlevel(Integer.parseInt(request.getParameter("blevel")));
		dto.setSubject(request.getParameter("subject"));
		dto.setWriter(request.getParameter("writer"));
		dto.setContent(request.getParameter("content"));
		dto.setPasswd(request.getParameter("pw"));
		
		BoardDAO dao = BoardDAO.getInstance();
		result = dao.UpdateArticle(dto);
		
		if(result>0) {
			msg = "수정 성공";
		} else {
			msg = "수정 실패";
		}
		page = "content.do?bno="+dto.getBno();
		
		request.setAttribute("msg", msg);
		request.setAttribute("page", page);
		
		return "view/MsgPage.jsp";
	}

}

 

action.WriteAction

더보기
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardDAO;
import model.BoardDTO;

public class WriteAction implements CommandAction {

	@Override
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		BoardDTO dto = new BoardDTO();
		
		dto.setSubject(request.getParameter("subject"));
		dto.setWriter(request.getParameter("writer"));
		dto.setContent(request.getParameter("content"));
		dto.setPasswd(request.getParameter("pw"));
		dto.setIp(request.getRemoteAddr());
		
		if(request.getParameter("bno") != null && request.getParameter("bno").length()!=0) {
			dto.setBno(Integer.parseInt(request.getParameter("bno")));
			dto.setBref(Integer.parseInt(request.getParameter("bref")));
			dto.setBstep(Integer.parseInt(request.getParameter("bstep")));
			dto.setBlevel(Integer.parseInt(request.getParameter("blevel")));
		}
		
		BoardDAO dao = BoardDAO.getInstance();
		
		int r = dao.writeArticle(dto);
		System.out.println(r);
		
		String msg = "";
		String page = "list.do";
		
		if(r>0) msg = "작성 성공";
		else msg = "작성 실패";
		System.out.println(msg);
		
		request.setAttribute("msg", msg);
		request.setAttribute("page", page);
		
		return "view/MsgPage.jsp";
	}

}

 

action.WriteFormAction

더보기
package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardDTO;

public class WriteFormAction implements CommandAction {

	@Override
	public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(request.getParameter("bno")!=null) {
			BoardDTO dto = new BoardDTO();
			dto.setBno(Integer.parseInt(request.getParameter("bno")));
			dto.setBref(Integer.parseInt(request.getParameter("bref")));
			dto.setBstep(Integer.parseInt(request.getParameter("bstep")));
			dto.setBlevel(Integer.parseInt(request.getParameter("blevel")));
			dto.setSubject(request.getParameter("subject"));
			request.setAttribute("dto", dto);
		}
			
		return "view/WriteForm.jsp";
	}

}

 

 

controller 전체 코드

controller.Controller

더보기
package controller;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import action.CommandAction;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Controller")
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Map<String, CommandAction> commandMap = new HashMap<String, CommandAction>(); 
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		String props = config.getInitParameter("propertyConfig");
		Properties pr = new Properties();
		BufferedInputStream bf = null;
		
		try {
			bf = new BufferedInputStream(new FileInputStream(props));
			pr.load(bf);
		} catch(IOException e){
			e.printStackTrace();
		} finally {
			if(bf!=null) try{bf.close();} catch(IOException ex) {ex.printStackTrace();}
		}
		
		Iterator<Object> keyIter = pr.keySet().iterator();
		
		while (keyIter.hasNext()) {
			String command = (String)keyIter.next();
			String classNm = pr.getProperty(command);
			
			try {
				Class<?> comClass = Class.forName(classNm);
				
				@SuppressWarnings("deprecation")
				CommandAction commInstance = (CommandAction) comClass.newInstance();
				commandMap.put(command, commInstance);
				
			} catch(ClassNotFoundException e) {
				e.printStackTrace();
			} catch(InstantiationException e) {
				e.printStackTrace();
			} catch(IllegalAccessException e) {
				e.printStackTrace();
			}
		};
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String view = null;
		CommandAction com = null;
		
		try {
			String command = request.getRequestURI();		
			if(command.indexOf(request.getContextPath())==0) {
				command = command.substring(request.getContextPath().length());
			}
			com = commandMap.get(command);
			view = com.requestProc(request, response);
		} catch(Exception e){
			e.printStackTrace();	
		}
		
		RequestDispatcher dp = request.getRequestDispatcher(view);
		dp.forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

 

Web-inf > Command.properties

더보기
/list.do=action.ListAction
/writeForm.do=action.WriteFormAction
/writeAction.do=action.WriteAction
/content.do=action.ContentAction
/update.do=action.UpdateAction
/updateProc.do=action.UpdateProc
/delete.do=action.DeleteAction

 

Web-inf > web.xml

더보기
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>JavaBoard</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
	
	<servlet>
		<servlet-name>Controller</servlet-name>
		<servlet-class>controller.Controller</servlet-class>
		<init-param>
			<param-name>propertyConfig</param-name>
			<param-value>/Users/_o_h_y_o_/Documents/coding/eclipse/JavaBoard/src/main/webapp/WEB-INF/Command.properties</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
    	<servlet-name>Controller</servlet-name>
    	<url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <resource-ref>
    	<description>JavaBoard DB</description>
    	<res-ref-name>jdbc/JavaBoard</res-ref-name>
    	<res-type>javax.sql.DataSource</res-type>
    	<res-auth>Container</res-auth>
    </resource-ref>

	<filter>
	    <filter-name>Encoding Filter</filter-name>
	    <filter-class>filter.CommonEncodingFilter</filter-class>
	    <init-param>
	        <param-name>encoding</param-name>
	        <param-value>UTF-8</param-value>
	    </init-param>
	</filter>
	<filter-mapping>
	    <filter-name>Encoding Filter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

 

 

 

model 전체 코드

model.BoardDAO

더보기
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BoardDAO {
	private static BoardDAO dao = null;
	
	private BoardDAO(){};
	
	public static BoardDAO getInstance() {
		if(dao==null) 
			dao = new BoardDAO();
		return dao;	
	}
	
	public int writeArticle(BoardDTO dto){
		int result = 0;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int newBno = 0;
		
		
		try {
			conn = DBConnector.getInstance().getConnection();
			String sql = "SELECT IFNULL(MAX(bno),0)+1 AS bno FROM board";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()) 
				newBno = rs.getInt(1);
			
			pstmt.close();
			
			sql = "INSERT INTO board"
					+ "(bno, bref, bstep, blevel, readcount, subject, "
					+ "content, writer, passwd, ip, regdate) "
					+ "VALUE(?,?,?,?,?,?,?,?,?,?,SYSDATE())";
			pstmt = conn.prepareStatement(sql);
			
			if(dto.getBno() > 0) {
				dto.setBno(newBno);
				dto.setBstep(dto.getBstep()+1);				
				dto.setBlevel(dto.getBlevel()+1);
			} else {
				dto.setBno(newBno);
				dto.setBref(newBno);
				dto.setBstep(newBno);
			}

			pstmt.setInt(1, dto.getBno());
			pstmt.setInt(2, dto.getBref());
			pstmt.setInt(3, dto.getBstep());
			pstmt.setInt(4, dto.getBlevel());
			pstmt.setInt(5, dto.getReadcount());
			pstmt.setString(6, dto.getSubject());
			pstmt.setString(7, dto.getContent());
			pstmt.setString(8, dto.getWriter());
			pstmt.setString(9, dto.getPasswd());
			pstmt.setString(10, dto.getIp());
			
			result = pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
			} catch(SQLException e) {
				e.printStackTrace();
			}
		}
		
		return result;
	}

	public int getTotalCnt() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int result = 0;
		
		try {
			conn = DBConnector.getInstance().getConnection();
			String sql = "SELECT COUNT(bno) AS cnt FROM board";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				result = rs.getInt("cnt");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
				if(rs!=null) rs.close();
			} catch(SQLException e) {e.printStackTrace();}
		}
		return result;
	}

	public List<BoardDTO> getArticles(int start, int end) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<BoardDTO> articles = new ArrayList<BoardDTO>();
		
		try {
			
			conn = DBConnector.getInstance().getConnection();
			String sql = "SELECT * FROM("
					+ "SELECT ROW_NUMBER() "
					+ "OVER(ORDER BY bref DESC, bno, bstep) AS rn, "
					+ "bno, bref, bstep, blevel, readcount, "
					+ "subject, content, writer, regdate, ip, passwd "
					+ "FROM board) t "
					+ "WHERE rn >= ? AND rn <= ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, start);
			pstmt.setInt(2, end);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
				BoardDTO article = new BoardDTO();
				article.setRn(rs.getInt("rn"));
				article.setBno(rs.getInt("bno"));
				article.setBref(rs.getInt("bref"));
				article.setBstep(rs.getInt("bstep"));
				article.setBlevel(rs.getInt("blevel"));
				article.setReadcount(rs.getInt("readcount"));
				article.setSubject(rs.getString("subject"));
				article.setContent(rs.getString("content"));
				article.setWriter(rs.getString("writer"));
				article.setRegdate(rs.getString("regdate"));
				article.setIp(rs.getString("ip"));
				article.setPasswd(rs.getString("passwd"));
				articles.add(article);
			}
			
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
				if(rs!=null) rs.close();
			} catch (SQLException e) {e.printStackTrace();}
		}
		
		return articles;
	}

	public BoardDTO getArticle(int bno) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int r = 0;
		BoardDTO article = new BoardDTO();
		
		try {
			conn = DBConnector.getInstance().getConnection();
			String sql = "SELECT bno, bref, bstep, blevel, readcount, "
					+ "subject, content, writer, regdate, ip, passwd "
					+ "FROM board WHERE bno=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, bno);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				article.setBno(rs.getInt("bno"));
				article.setBref(rs.getInt("bref"));
				article.setBstep(rs.getInt("bstep"));
				article.setBlevel(rs.getInt("blevel"));
				article.setReadcount(rs.getInt("readcount"));
				article.setSubject(rs.getString("subject"));
				article.setContent(rs.getString("content"));
				article.setWriter(rs.getString("writer"));
				article.setRegdate(rs.getString("regdate"));
				article.setIp(rs.getString("ip"));
				article.setPasswd(rs.getString("passwd"));
			}
			System.out.println(article);
			pstmt.close();
			
			
			// 조회수 증가
			sql = "UPDATE board SET readcount = readcount+1 WHERE bno=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, bno);
			r = pstmt.executeUpdate();
			System.out.println(r);
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("여기?");
		} finally {
			try {
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
				if(rs!=null) rs.close();
			} catch(SQLException e) {e.printStackTrace();}
		}
		
		return article;
	}

	public int UpdateArticle(BoardDTO dto) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int r = 0;
		
		try {
			conn = DBConnector.getInstance().getConnection();
			String sql = "UPDATE board SET "
					+ "subject = ?, writer = ?, content = ?, passwd = ? "
					+ "WHERE bno = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, dto.getSubject());
			pstmt.setString(2, dto.getWriter());
			pstmt.setString(3, dto.getContent());
			pstmt.setString(4, dto.getPasswd());
			pstmt.setInt(5, dto.getBno());
			
			r = pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return r;
	}
	
	public int deleteArticle(int bno) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int r = 0;
		
		try {
			conn = DBConnector.getInstance().getConnection();
			String sql = "DELETE FROM board WHERE bno=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, bno);
			r = pstmt.executeUpdate();
			
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return r;
	}
}

 

model.BoardDTO

더보기
package model;

public class BoardDTO {
	private int rn;
	private int bno;
	private int bref;
	private int bstep;
	private int blevel;
	private int readcount;
	private String subject;
	private String content;
	private String writer;
	private String regdate;
	private String ip;
	private String passwd;
	
	public int getRn() {
		return rn;
	}
	public void setRn(int rn) {
		this.rn = rn;
	}
	public int getBno() {
		return bno;
	}
	public void setBno(int bno) {
		this.bno = bno;
	}
	public int getBref() {
		return bref;
	}
	public void setBref(int bref) {
		this.bref = bref;
	}
	public int getBstep() {
		return bstep;
	}
	public void setBstep(int bstep) {
		this.bstep = bstep;
	}
	public int getBlevel() {
		return blevel;
	}
	public void setBlevel(int blevel) {
		this.blevel = blevel;
	}
	public int getReadcount() {
		return readcount;
	}
	public void setReadcount(int readcount) {
		this.readcount = readcount;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getRegdate() {
		return regdate;
	}
	public void setRegdate(String regdate) {
		this.regdate = regdate;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	
	
}

 

model.DBConnector

더보기
package model;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBConnector {
	private static DBConnector dbconn = null;
	private DBConnector() {}
	
	public static DBConnector getInstance() {
		if(dbconn==null)
			dbconn = new DBConnector();
		return dbconn;
	}
	
	public Connection getConnection() throws NamingException, SQLException{
		Context initCtx = new InitialContext();
		Context envCtx = (Context) initCtx.lookup("java:comp/env");
		DataSource ds = (DataSource)envCtx.lookup("jdbc/JavaBoard");
		return ds.getConnection();
	}
}

 

model.PageBean

더보기
package model;

public interface PageBean {
	final int LINE_OF_PAGE = 5;
}

 

Servers > JavaBoard-config > context.xml

더보기
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <Resource auth="Container" 
      name="jdbc/JavaBoard" 
      driverClassName="com.mysql.jdbc.Driver" 
      type="javax.sql.DataSource" 
      url="jdbc:mysql://localhost/board" 
      username="root"
      password="70657065" 
      loginTimeout="10" 
      maxActive="50" 
      maxIdle="20"
      maxWait="5000" 
      testOnBorrow="true" />

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
</Context>

 

 

 

view 전체 코드

view.ContentView.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글보기</title>
<link rel="stylesheet" type="text/css" href="css/form.css">
<script src="jquery/jquery-3.7.0.min.js"></script>
<script type="text/javascript">
$().ready(function(){
	let f = $('form[name=form1]');
	$("#update").on("click", function(){
		if(checkpw()){
			f.attr("action","update.do");
			f.submit();
		} else 
			alert("비밀번호가 틀렸습니다.");
	})
	$("#delete").on("click", function(){
		if(checkpw()){
			if(confirm("정말 삭제하시겠습니까?"))
			f.attr("action","delete.do")
			f.submit();
		} else
			alert("비밀번호가 틀렸습니다.");
	})
	
	$("#reply").on("click", function(){
		f.attr('action','writeForm.do');
		f.submit();
	})
})

function checkpw(){
	let upw = prompt("비밀번호를 입력하시오.");
	let cpw = $('input[name=pw]').val();
	if(upw==cpw)
		return true;
	return false;
}
</script>
</head>
<body>
<form name="form1" action="" method="post">
	<table>
		<tr><td colspan="2"><h3>${article.subject}</h3></td></tr>
		<tr>
			<th>작성자</th>
			<td><input type="text" name="writer" class="chk" title="작성자" readonly="readonly" value="${article.writer}"></td>
		</tr>
		<tr>
			<th>작성일자</th>
			<td><input type="text" name="regdate" class="chk" title="작성일자" readonly="readonly" value="${article.regdate}"></td>
		</tr>
		<tr>
			<th>IP</th>
			<td><input type="text" name="ip" class="chk" title="IP" readonly="readonly" value="${article.ip}"></td>
		</tr>
		<tr>
			<th>내용</th>
			<td><textarea name="content" class="chk" readonly="readonly" title="내용">${article.content}</textarea></td>
		</tr>
		<tr>
			<th>비밀번호</th>
			<td><input type="password" name="pw" class="chk" title="비밀번호" readonly="readonly" value="${article.passwd}"></td>
		</tr>
		<tr>
			<td colspan="3">
				<input type="button" class="btn" value="글 수정" id="update">
				<input type="button" class="btn" value="답글" id="reply">
				<input type="button" class="btn" value="글 삭제" id="delete">
				<input type="button" class="btn" value="글목록" onclick="location.href='list.do'">
			</td>
		</tr>
	</table>
	<input type="hidden" name="bno" value="${article.bno}">
	<input type="hidden" name="bref" value="${article.bref}">
	<input type="hidden" name="bstep" value="${article.bstep}">
	<input type="hidden" name="blevel" value="${article.blevel}">
	<input type="hidden" name="subject" value="${article.subject}">
</form>
</body>
</html>

 

view.ListView.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글 목록</title>
<link rel="stylesheet" type="text/css" href="css/list.css">
<script src="jquery/jquery-3.7.0.min.js"></script>
<script>
$().ready(function(){
	let $item = $('a.content').on("click",function(){
		let idx = $item.index(this);
		//alert($("a.content input[type=hidden]").eq(idx).val());
		
		var f = $('form[name=content]')
		$('form input[name=bno]').attr('value', $("a.content input[name=no]").eq(idx).val());
		f.attr('action','content.do');
		f.submit();
	});
})
</script>
</head>
<body>
<table>
	<thead>
		<tr>
			<th colspan="5"><h3>글 목록 보기 (전체 게시글 수 : ${totalCnt})</h3></th>
			<th><a href="writeForm.do">글쓰기</a></th>
		</tr>
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>작성자</th>
			<th>최종 수정일</th>
			<th>조회수</th>
			<th>아이피</th>
		</tr>
	</thead>
	<tbody>
		<c:choose>
			<c:when test="${fn:length(articles)==0}">
				<tr><td colspan="6"><span>게시글이 없습니다.</span></td></tr>
			</c:when>
			<c:when test="${fn:length(articles)>0}">
				<c:forEach items="${articles}" var="article" varStatus="i">
					<tr>
						<td class="col1">${article.rn}</td>
						<td class="col2">
							<c:choose>
								<c:when test="${article.blevel==0}">
									<img src="image/level.gif" style="width:5px; height:10px;">
								</c:when>
								<c:when test="${article.blevel>0}">
									<img src="image/level.gif" width=${article.blevel*15}px>
									<img src="image/re.gif">
								</c:when>
							</c:choose>
							<a class="content">
								${article.subject}
								<input type="hidden" name="no" value="${article.bno}">
							</a>
						</td>
						<td class="col3">${article.writer}</td>
						<td class="col4">${article.regdate}</td>
						<td class="col5">
							${article.readcount}
							<c:if test="${article.readcount>10}">
								<img src="image/hot.gif" style="width:15px; height:10px;">
							</c:if>
						</td>
						<td class="col6">${article.ip}</td>
					</tr>
				</c:forEach>
			</c:when>
		</c:choose>
	</tbody>
	<tfoot>
		<tr>
			<td colspan="6" style="text-align:center">
				<c:forEach var="p" begin="1" end="${pageCnt}" step="1">
					<a href="list.do?p=${p}"><c:out value="${p}"/></a> &nbsp;
				</c:forEach>
			</td>
		</tr>
	</tfoot>
</table>
<form action="content.do" method="post" name="content">
	<input type="hidden" name="bno" value="${article.bno}">
</form>
</body>
</html>

 

view.UpdateView.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글보기</title>
<link rel="stylesheet" type="text/css" href="css/form.css">
<script src="jquery/jquery-3.7.0.min.js"></script>
<script type="text/javascript">
$().ready(function(){
	$("#submit1").on("click",function(){
		if(validate()){
			$("form[name=updateform]").submit();
		}
	});
});

function validate(){
	let f = $("form[name=updateform] .chk");
	let flen = f.length;
	for(let i=0;i<flen;i++){
		if(f.eq(i).val()==null ||
				f.eq(i).val().length==0 ||
				f.eq(i).val().trim()==""){
			alert(f.eq(i).attr("title")+"의 입력은 필수입니다.");
			f.eq(i).focus();
			return false;
		}
	}
	return true;
}
</script>
</head>
<body>
<form name="updateform" action="updateProc.do" method="post">
	<table>
		<tr>
			<th>제목</th>
			<td><input type="text" name="subject" class="chk" title="제목" value="${article.subject}"></td>
		</tr>
		<tr>
			<th>작성자</th>
			<td><input type="text" name="writer" class="chk" title="작성자" value="${article.writer}"></td>
		</tr>
		<tr>
			<th>내용</th>
			<td><textarea name="content" class="chk" title="내용">${article.content}</textarea></td>
		</tr>
		<tr>
			<th>비밀번호</th>
			<td><input type="password" name="pw" class="chk" title="비밀번호" value="${article.passwd}"></td>
		</tr>
		<tr>
			<td colspan="3">
				<input type="button" class="btn" value="글 수정" id="submit1">
				<input type="button" class="btn" value="글목록" onclick="location.href='list.do'">
			</td>
		</tr>
	</table>
	<input type="hidden" name="bno" value="${article.bno}">
	<input type="hidden" name="bref" value="${article.bref}">
	<input type="hidden" name="bstep" value="${article.bstep}">
	<input type="hidden" name="blevel" value="${article.blevel}">
</form>
</body>
</html>

 

view/WriteForm.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글쓰기</title>
<link rel="stylesheet" type="text/css" href="css/form.css">
<script src="jquery/jquery-3.7.0.min.js"></script>
<script type="text/javascript">
	$().ready(function(){
		$("#submit1").on("click",function(){
			if(validate()){
				$("form").submit();
			}
		});
	});
	
	function validate(){
		let f = $("form[name=writeform] .chk");
		let flen = f.length;
		for(let i=0;i<flen;i++){
			if(f.eq(i).val()==null ||
					f.eq(i).val().length==0 ||
					f.eq(i).val().trim()==""){
				alert(f.eq(i).attr("title")+"의 입력은 필수입니다.");
				f.eq(i).focus();
				return false;
			}
		}
		return true;
	}
</script>
</head>
<body>
<form name="writeform" action="writeAction.do" method="post">
	<table>
		<tr><td colspan="2"><h3>게시글 쓰기</h3></td></tr>
		<tr>
			<th>제목</th>
			<c:choose>
				<c:when test="${dto.bno==0}">
					<td><input type="text" name="subject" class="chk" title="제목"></td>
				</c:when>
				<c:when test="${dto.bno>0}">
					<td><input type="text" name="subject" class="chk" title="제목" value="[답글] ${dto.subject}"></td>
				</c:when>
			</c:choose>
		</tr>
		<tr>
			<th>작성자</th>
			<td><input type="text" name="writer" class="chk" title="작성자"></td>
		</tr>
		<tr>
			<th>내용</th>
			<td><textarea name="content" class="chk" title="내용"></textarea></td>
		</tr>
		<tr>
			<th>비밀번호</th>
			<td><input type="password" name="pw" class="chk" title="비밀번호"></td>
		</tr>
		<tr>
			<td colspan="3">
				<input type="button" class="btn" value="글 작성" id="submit1">
				<input type="reset" class="btn" value="다시 쓰기">
				<input type="button" class="btn" value="글목록" onclick="location.href='list.do'">
			</td>
		</tr>
	</table>
	<input type="hidden" name="bno" value="${dto.bno}">
	<input type="hidden" name="bref" value="${dto.bref}">
	<input type="hidden" name="bstep" value="${dto.bstep}">
	<input type="hidden" name="blevel" value="${dto.blevel}">
</form>
</body>
</html>

 

view.MsgPage.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<script src="jquery/jquery-3.7.0.min.js"></script>
<script type="text/javascript">
$().ready(function(){
	let msg = "${msg}";
	let page = "${page}";

	alert(msg);
	$(location).attr("href",page);
})

</script>

 

css.form.css

더보기
@charset "UTF-8";
* {margin:0; padding:0;}

table {
	width:80%;
	margin: 0 auto;
	border:1px solid black;
	background-color: #cfcfff;
	padding:10px 30px 10px 38px;
}

table th {border : 1px solid balck;}

table tr {text-align:center;}

table th {
	text-align:center;
	border:1px solid #696969;
	background-color: #ffffff;
	border-radius: 2px;
}

input {width:90%; padding:3px;}

textarea {
	width:90%; height:300px; padding:3px;
	resize: none;	
}

.btn {
	width:100px;
	height:30px;
	font-weight: bold;
	margin-top:15px;
	margin-bottom:5px;
	background-color: #ffffff;
	border:1px solid #696969;
}

h3 {margin:5px 5px 10px 5px;}

 

css.list.css

더보기
@charset "UTF-8";
*{margin:0; padding: 0;}
table {border:1px solid black; border-collapse: collapse; width:90%; margin:0 auto;}
th, td {border:1px solid black; border-collapse: collapse; padding:10px; margin:5px;}
th {background-color: #cfcfff;}
td {background-color: #ffffff; font-weight: 200;}
.btn {height:30px; width:850px; text-align:right;}
a {color:black; text-decoration: none; }
a:hover {font-weight: bold;}
.col1 {width:8%;}
.col2 {width:35%;}
.col3 {width:20%;}
.col4 {width:15%;}
.col5 {width:10%;}
.col6 {width:12%;}
span {text-align: center;}