2016년 8월 18일 목요일

13day JSP

Model1  → Model2

  • Model1
board_write1.jsp      → BoardTO  → BoardDAO  → DB
board_write1_ok.jsp
board_list1.jsp
board_view1.jsp


  • Model2

controller(servlet)   → Action(interface) 상속   → BoardTO  → BoardDAO  → DB
                             WriteAction
                             WriteOkAction
                             ListAction
                             ViewAction
                             ...                        
                        ←  board_write1.jsp
                             board_write1_ok.jsp
                             board_list1.jsp
                             board_view1.jsp
                             ...


1model2
폴더 구성

데이터,데이터처리 클래스는 model1그대로 사용
jsp페이지는 직접접근이 불가능한 WEB-INF폴더에 넣음
model2에서 컨트롤할 서블릿 페이지와
데이터 처리할 action페이지가 추가됨

2.model1과 동일한 파일
context.xml :DB pooling연결
<?xml version="1.0" encoding="utf-8" ?>
<Context>
    <Resource 
        name="jdbc/oracle" 
        auth="Container" 
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver" 
        url="jdbc:oracle:thin:@localhost:1521:orcl"
        username="project" 
        password="project" 
        maxTotal="20" 
        maxWaitMillis="5000" />    
</Context>

BoardTO : 데이터용 클래스
package model1;

// 데이터용 클래스 만듦
public class BoardTO {
 private String seq;
 private String subject;
 private String writer;
 private String mail;
 private String password;
 private String content;
 private String hit;
 private String wip;
 private String wdate;
 private int wgap;
 
 public String getSeq() {
  return seq;
 }
 public String getSubject() {
  return subject;
 }
 public String getWriter() {
  return writer;
 }
 public String getMail() {
  return mail;
 }
 public String getPassword() {
  return password;
 }
 public String getContent() {
  return content;
 }
 public String getHit() {
  return hit;
 }
 public String getWip() {
  return wip;
 }
 public String getWdate() {
  return wdate;
 }
 public int getWgap() {
  return wgap;
 }
 public void setSeq(String seq) {
  this.seq = seq;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public void setWriter(String writer) {
  this.writer = writer;
 }
 public void setMail(String mail) {
  this.mail = mail;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public void setContent(String content) {
  this.content = content;
 }
 public void setHit(String hit) {
  this.hit = hit;
 }
 public void setWip(String wip) {
  this.wip = wip;
 }
 public void setWdate(String wdate) {
  this.wdate = wdate;
 }
 public void setWgap(int wgap) {
  this.wgap = wgap;
 }
 
 
}


BoardDAO : 데이터 처리클래스
package model1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BoardDAO {
    private  DataSource dataSource;
    
    //생성자에서 tomcat pooling 설정
    public BoardDAO() {
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context)initCtx.lookup("java:comp/env");
            this.dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        } catch (NamingException e) {
            System.out.println("에러 : "+ e.getMessage());
        }
    }
    
    /*각 페이지에 필요한 데이터를 가져오는 메서드를 정의*/
    public void boardWrite(){
        //필요없음
    }
    //
    public int boardWriteOk(BoardTO to){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int flag = 1;
        
        try{
            conn = this.dataSource.getConnection();

            String sql = "insert into board1 values (board_seq.nextval, ?, ?, ?, ?, ?, 0, ?, sysdate)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSubject());
            pstmt.setString(2, to.getWriter());
            pstmt.setString(3, to.getMail());
            pstmt.setString(4, to.getPassword());
            pstmt.setString(5, to.getContent());
            pstmt.setString(6, to.getWip());

            if(pstmt.executeUpdate() ==1){
                flag =0;
            }
        }catch(SQLException e){
            System.out.println("에러 : " + e.getMessage());
        }finally{
            if(pstmt !=null)try{pstmt.close();}catch(SQLException e){};
            if(conn !=null)try{conn.close();}catch(SQLException e){};
        }
        return flag;
    }
    //
    public ArrayList<BoardTO> boardList(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        ArrayList<BoardTO> result = new ArrayList<>();
        
        try{
            conn = dataSource.getConnection();
            
            String sql = "select seq, subject, writer, to_char(wdate,'YY.MM.DD') wdate, hit, trunc(sysdate-wdate) wgap from board1 order by seq desc";
            pstmt = conn.prepareStatement(sql);
            rs= pstmt.executeQuery();
            while(rs.next()){
                BoardTO to = new BoardTO();
                to.setSeq(rs.getString("seq"));
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setWdate(rs.getString("wdate"));
                to.setHit(rs.getString("hit"));
                to.setWgap(rs.getInt("wgap"));
                result.add(to);
            }
            
        }catch(SQLException e){
            System.out.println("에러 : " + e.getMessage());
        }finally{
            if(rs!=null)try {rs.close();} catch (SQLException e) {}
            if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
            if(conn!=null)try {conn.close();} catch (SQLException e) {}
        }
        return result;
    }
    //
    public BoardTO boardView(BoardTO to){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try{
            conn = this.dataSource.getConnection();
            
            //조회수 증가
            String sql = "update board1 set hit=hit+1 where seq=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            pstmt.executeUpdate();
            
            //view창 출력
            sql = "select subject, writer, mail, wip, wdate, hit, content from board1 where seq=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            
            rs = pstmt.executeQuery();
            if(rs.next()){
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setMail(rs.getString("mail")==null?"":rs.getString("mail"));
                to.setWip(rs.getString("wip"));
                to.setWdate(rs.getString("wdate"));
                to.setHit(rs.getString("hit"));
                to.setContent(rs.getString("content")==null?"":rs.getString("content").replaceAll("\n", "<br>")); //엔터키 처리
            }
        }catch(SQLException e){
            System.out.println("에러 : " + e.getMessage());
        }finally{
            if(rs!=null)try {rs.close();} catch (SQLException e) {}
            if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
            if(conn!=null)try {conn.close();} catch (SQLException e) {}
        }
        return to;
    }
    //
    public BoardTO boardModify(BoardTO to){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try{
            conn = this.dataSource.getConnection();
            
            //modify창 출력
            String sql = "select subject, writer, mail,content from board1 where seq=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            
            rs = pstmt.executeQuery();
            if(rs.next()){
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setMail(rs.getString("mail")==null?"":rs.getString("mail"));
                to.setContent(rs.getString("content")==null?"":rs.getString("content").replaceAll("\n", "<br>"));
            }
        }catch(SQLException e){
            System.out.println("에러 : " + e.getMessage());
        }finally{
            if(rs!=null)try {rs.close();} catch (SQLException e) {}
            if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
            if(conn!=null)try {conn.close();} catch (SQLException e) {}
        }
        return to;
    }
    //
    public int boardModifyOk(BoardTO to){
        Connection conn = null;
        PreparedStatement pstmt = null;
       
        int flag = 2;
        try{
            conn = this.dataSource.getConnection();
            
            String sql = "update board1 set subject=?, mail=?, content=? where seq=? and password=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSubject());
            pstmt.setString(2, to.getMail());
            pstmt.setString(3, to.getContent());
            pstmt.setString(4, to.getSeq());
            pstmt.setString(5, to.getPassword());
            
            int result = pstmt.executeUpdate();
            if(result ==0){
                flag =1;
            }else if(result ==1){
                flag =0;
            }
        }catch(SQLException e){
            System.out.println("에러 : " + e.getMessage());
        }finally{
            if(pstmt !=null)try{pstmt.close();}catch(SQLException e){};
            if(conn !=null)try{conn.close();}catch(SQLException e){};
        }
        return flag;
    }
    //
    public BoardTO boardDelete(BoardTO to){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try{
            conn = this.dataSource.getConnection();
            
            //delete창 출력
            String sql = "select subject, writer, mail from board1 where seq=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            
            rs = pstmt.executeQuery();
            if(rs.next()){
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setMail(rs.getString("mail")==null?"":rs.getString("mail"));
            }
        }catch(SQLException e){
            System.out.println("에러 : " + e.getMessage());
        }finally{
            if(rs!=null)try {rs.close();} catch (SQLException e) {}
            if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
            if(conn!=null)try {conn.close();} catch (SQLException e) {}
        }
        return to;
    }
    //
    public int boardDeleteOk(BoardTO to){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int flag = 2;
        
        try{
            conn = this.dataSource.getConnection();
            //delete 처리
            String sql = "delete from board1 where seq=? and password=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            pstmt.setString(2, to.getPassword());
            
            int result = pstmt.executeUpdate();
            if(result ==0){
                flag =1;
            }else if(result ==1){
                flag =0;
            }
        }catch(SQLException e){
            System.out.println("에러 : " + e.getMessage());
        }finally{
            if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
            if(conn!=null)try {conn.close();} catch (SQLException e) {}
        }
        return flag;
    }
}


3.controller 서블릿
package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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 model2.Action;
import model2.DeleteAction;
import model2.DeleteOkAction;
import model2.ListAction;
import model2.ModifyAction;
import model2.ModifyOkAction;
import model2.ViewAction;
import model2.WriteAction;
import model2.WriteOkAction;

/**
 * Servlet implementation class BoardController
 */
@WebServlet("*.do")
public class BoardController extends HttpServlet {
 private static final long serialVersionUID = 1L;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doProcess(request, response);
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doProcess(request, response);
 }
 
 protected void doProcess(HttpServletRequest request, HttpServletResponse response) {
  try {
   request.setCharacterEncoding("utf-8");
   
   String path = request.getRequestURI().replaceAll(request.getContextPath(), "");
   String url = "";
   Action action = null;
   
   //가상의 URL주소가 요청되면 Action처리후, 접속할 실제 폴더 위치 정함
   if(path.equals("/*.do") || path.equals("/list.do")){
    action = new ListAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_list1.jsp";
   }else if(path.equals("/view.do")){
    action = new ViewAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_view1.jsp";
   }else if(path.equals("/write.do")){
    action = new WriteAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_write1.jsp";
   }else if(path.equals("/write_ok.do")){
    action = new WriteOkAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_write1_ok.jsp";
   }else if(path.equals("/modify.do")){
    action = new ModifyAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_modify1.jsp";
   }else if(path.equals("/modify_ok.do")){
    action = new ModifyOkAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_modify1_ok.jsp";
   }else if(path.equals("/delete.do")){
    action = new DeleteAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_delete1.jsp";
   }else if(path.equals("/delete_ok.do")){
    action = new DeleteOkAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_delete1_ok.jsp";
   }
   
   //실제 URL주소 내용을 가상 URL에 띄움
   if(!url.equals("")){
    RequestDispatcher dispatcher = request.getRequestDispatcher(url);
    dispatcher.forward(request, response);
   }
  } catch (UnsupportedEncodingException e) {
   System.out.println("에러 : " +e.getMessage());
  } catch (ServletException e) {
   System.out.println("에러 : " +e.getMessage());
  } catch (IOException e) {
   System.out.println("에러 : " +e.getMessage());
  }
 }

}


Action interface
package model2;

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

public interface Action {
 public abstract void execute(HttpServletRequest request, HttpServletResponse response);
}


4. WriteAction
package model2;

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

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class WriteAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  // 내용없음
 }

}

board_write1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
<script type="text/javascript">
    function ChkForm() {
        //필수 입력값 검사
        if(document.wfrm.subject.value.trim() ==""){
            alert('제목을 입력하셔야 합니다.');
            return false;
        }
        if(document.wfrm.writer.value.trim() ==""){
            alert('이름을 입력하셔야 합니다.');
            return false;
        }
        if(document.wfrm.password.value.trim() ==""){
            alert('암호를 입력하셔야 합니다.');
            return false;
        }
    }
</script>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <form action='./write_ok.do' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>제목</font> :
            </td>
            <td>
                <input type='text' name='subject' size='80' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' size='10' maxlength='10' class='form'>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' size='40' maxlength='70' class='form'>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<input type='password' name='password' size='10' maxlength='10' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>내용</font> :
            </td>
            <td>
                <textarea name='content' style='width:620;height:300' class='form'></textarea>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>

        <table width='100%' cellpadding='0' cellspacing='0' border='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='./images/btn_wri.gif' border='0'>&nbsp;
                <a href='./list1.do'><img src='./images/btn_list.gif' border='0'></a>
            </td>
        </tr>
        </table>
        </form>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


5. WriteOkAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class WriteOkAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  
  //데이터 클래스를 생성, 클라이언트 정보 넣음
  BoardTO to = new BoardTO();
  to.setSubject(request.getParameter("subject"));
  to.setWriter(request.getParameter("writer"));
  to.setMail(request.getParameter("mail"));
  to.setPassword(request.getParameter("password"));
  to.setContent(request.getParameter("content"));
  to.setWip(request.getRemoteAddr());
  //DAO클래스에서 DB데이터를 처리해서 리턴
  BoardDAO dao = new BoardDAO();
  int flag = dao.boardWriteOk(to);
  
  //데이터 넘김
  request.setAttribute("flag", flag);
 }

}

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

<%
    //디자인에 필요한 최소한의 데이터를 받음
       int flag = (Integer)request.getAttribute("flag");
    
      //성공실패 알림창   
    out.println("<script type='text/javascript'>");
    if(flag ==0){
        out.println("alert('글쓰기에 성공했습니다')");
        out.println("location.href='./list.do';");
    }else{
        out.println("alert('글쓰기에 실패했습니다')");
        out.println("history.back()");
    }
    out.println("</script>");
    
%>


6. ListAction
package model2;

import java.util.ArrayList;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ListAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //DAO클래스에서 list에서 필요한 DB데이터를 처리해서 ArrayList로 리턴
  BoardDAO dao = new BoardDAO();
  ArrayList lists = dao.boardList();
  
  //데이터 넘김
  request.setAttribute("lists", lists);
 }

}

board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="java.util.ArrayList"%>

<%
    //ListAction이 보낸 데이터 받음
    ArrayList<BoardTO> lists = (ArrayList)request.getAttribute("lists");    

    //ArrayList 데이터 result에 담음
    StringBuffer result = new StringBuffer();
    for(BoardTO to: lists){
        String seq = to.getSeq();
        String subject = to.getSubject();
        String writer = to.getWriter();
        String wdate = to.getWdate();
        String hit = to.getHit();
        int wgap = to.getWgap();        
            
        result.append("<table width='100%' border='0' cellpadding='0' cellspacing='0'>");
        result.append("<tr>");
        result.append("    <td height='1'></td>");
        result.append("</tr>");
        result.append("<tr>");
        result.append("    <td>");
        result.append("        <table width='100%' border='0' cellpadding='0' cellspacing='0'>");
        result.append("            <tr height='25' onMouseOver=\"this.className='evencell'\" onMouseOut=\"this.className=''\">");
        result.append("                <td width='40' align='center'>"+seq+"</td>");
        result.append("                <td>");
        result.append("                    <span style='width:370' class='elltxt'>");
        result.append("                        <a href='./view.do?seq="+seq+"'>"+subject+"</a>");
          //new이미지 24시간
        if(wgap == 0) {
            result.append("                <img src='./images/ico_n.gif' width='8' height='8' border='0' hspace='3'>");
        }
        result.append("                    </span>");
        result.append("                </td>");
        result.append("                <td width='100' align='center'>"+writer+"</td>");
        result.append("                <td width='80' align='center'>"+wdate+"</td>");
        result.append("                <td width='50' align='center'>"+hit+"</td>");
        result.append("            </tr>");
        result.append("        </table>");
        result.append("    </td>");
        result.append("</tr>");
        result.append("<tr>");
        result.append("    <td height='1'></td>");
        result.append("</tr>");
        result.append("<tr>");
        result.append("    <td align='center' class='imgline'></td>");
        result.append("</tr>");
        result.append("</table>");  
    }
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td align='center' class='gline'></td>
        </tr>
        <tr>
            <td align='center'>
                <table width='100%' border='0' cellpadding='0' cellspacing='0' class='titlecell'>
                <tr height='25' align='center'>
                    <td width='40'>No</td>
                    <td>제목</td>
                    <td width='100'>이름</td>
                    <td width='80'>등록일</td>
                    <td width='50'>조회수</td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align='center' class='gline'></td>
        </tr>
        <tr>
            <td height='3'></td>
        </tr>
        </table>
        
<!-- 리스트 시작 -->
<%= result %>
<!-- 리스트 끝 -->

        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <a href='./write.do'><img src='./images/btn_wri.gif' border='0'></a>
            </td>
        </tr>
        </table>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


7. ViewAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ViewAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //선택글 번호 받아서 TO에 넣음
  BoardTO to = new BoardTO();
     to.setSeq(request.getParameter("seq"));
     
     //DAO View에서 필요한 DB데이터를 처리해서 리턴
     BoardDAO dao = new BoardDAO();
     to = dao.boardView(to); 
     
     //데이터 넘김
     request.setAttribute("to", to);
 }

}

board_view1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%
    //Action에서 디자인에 필요한 최소한의 데이터 받음
    BoardTO to = (BoardTO)request.getAttribute("to");
    
    String seq = to.getSeq();
    String subject =to.getSubject();
    String writer = to.getWriter();
    String mail = to.getMail();
    String wip = to.getWip();
    String wdate = to.getWdate();
    String hit = to.getHit();
    String content = to.getContent();

%>

<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
        <tr>
            <td height='25'><font class='titdot'>&#149;&nbsp;</font><font class='title'>제목</font> | <%=subject %></td>
        </tr>
        <tr>
            <td class='imgline'></td>
        </tr>
        <tr>
            <td height='25'><font class='titdot'>&#149;&nbsp;</font><font class='title'>이름</font> | <%=writer %>(<%=mail %>) (<%=wip %>)</td>
        </tr>
        <tr>
            <td class='imgline'></td>
        </tr>
        <tr>
            <td height='25'>
                <table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
                <tr>
                    <td><font class='titdot'>&#149;</font><font class='title'> 날짜</font> | <%=wdate %></td>
                    <td align='right'><font class='titdot'>&#149;&nbsp;</font><font class='title'>조회</font> | <%=hit %></td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td  class='imgline'></td>
        </tr>
        <tr>
            <td height='100'><%=content %></td>
        </tr>
        </table>

        <table width='100%' border='0' cellspacing='0' cellpadding='0' align='center'>
        <tr>
            <td id='tailarea'></td>
        </tr>
        </table>

        <table width='100%' border='0' cellspacing='1' cellpadding='1'>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        <tr>
            <td width='500' height='30'>
                <a href='./write.do'><img src='./images/btn_wri.gif' border='0'></a>                  
                <a href='./modify.do?seq=<%=seq%>'><img src='./images/btn_mod.gif' border='0'></a> <%-- 글수정 URL seq녛기 --%>
                <a href='./delete.do?seq=<%=seq%>'><img src='./images/btn_del.gif' border='0'></a> <%-- 글삭제 URL seq녛기 --%>
            </td>
            <td align='right'>
                <a href='./list.do'><img src='./images/btn_list.gif' border='0'></a>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


8. ModifyAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ModifyAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //선택글 번호 받아서 TO에 넣음
  BoardTO to = new BoardTO();
   to.setSeq(request.getParameter("seq"));
   
   //DAO modify에서 필요한 DB데이터를 처리해서 리턴
     BoardDAO dao = new BoardDAO();
     to = dao.boardModify(to);
     
     //데이터 넘김
     request.setAttribute("to", to);
 }

}

board_modify1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
<script type="text/javascript">
    function ChkForm() {
        //필수 입력값 검사
        if(document.wfrm.subject.value.trim() ==""){
            alert('제목을 입력하셔야 합니다.');
            return false;
        }
        if(document.wfrm.writer.value.trim() ==""){
            alert('이름을 입력하셔야 합니다.');
            return false;
        }
        if(document.wfrm.password.value.trim() ==""){
            alert('암호를 입력하셔야 합니다.');
            return false;
        }
    }
</script>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <form action='./write_ok.do' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>제목</font> :
            </td>
            <td>
                <input type='text' name='subject' size='80' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' size='10' maxlength='10' class='form'>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' size='40' maxlength='70' class='form'>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<input type='password' name='password' size='10' maxlength='10' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>내용</font> :
            </td>
            <td>
                <textarea name='content' style='width:620;height:300' class='form'></textarea>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>

        <table width='100%' cellpadding='0' cellspacing='0' border='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='./images/btn_wri.gif' border='0'>&nbsp;
                <a href='./list1.do'><img src='./images/btn_list.gif' border='0'></a>
            </td>
        </tr>
        </table>
        </form>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


9. ModifyOkAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ModifyOkAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 클래스를 생성, 클라이언트 정보 넣음
  BoardTO to = new BoardTO();
  to.setSeq(request.getParameter("seq"));
  to.setSubject(request.getParameter("subject"));
  to.setMail(request.getParameter("mail"));
  to.setPassword(request.getParameter("password"));
  to.setContent(request.getParameter("content"));

  //DAO클래스에서 modify_ok에서 필요한 DB데이터를 처리해서 리턴
  BoardDAO dao = new BoardDAO();
     int flag = dao.boardModifyOk(to);
     
  //데이터 넘김
     request.setAttribute("flag", flag);
 }

}

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

<%
    //Action에서 처리된 최소한의 데이터 받음
    int flag = (Integer)request.getAttribute("flag");
    
     //성공실패 메시지
    out.println("<script type='text/javascript'>");
    if(flag ==0){
        out.println("alert('글수정에 성공했습니다')");
        out.println("location.href='list.do';");
    }else if(flag==1){
        out.println("alert('비밀번호가 잘못되었습니다.')");
        out.println("history.back();");
    }else{
        out.println("alert('글수정에 실패했습니다')");
        out.println("history.back();");
    }
    out.println("</script>");
    
%>


10. DeleteAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class DeleteAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //선택글 번호 받아서 TO에 넣음
  BoardTO to = new BoardTO();
  to.setSeq(request.getParameter("seq"));
  
  //DAO delete에서 필요한 DB데이터를 처리해서 리턴
     BoardDAO dao = new BoardDAO();
     to = dao.boardDelete(to);
     
  //데이터 넘김
     request.setAttribute("to", to);
 }

}

board_delete1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>

<%
    //action에서 처리된 최소한의 데이터 받음
    BoardTO to = (BoardTO)request.getAttribute("to");
    
    String seq = to.getSeq();
    String subject =to.getSubject();
    String writer = to.getWriter();
    String mail = to.getMail();
%>

<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
<script type="text/javascript">
    function ChkForm(form) {
        //필수 입력값 검사
        if(document.wfrm.password.value.trim() ==""){
            alert('암호를 입력하셔야 합니다.');
            return false;
        }
    }
</script>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <form action='./delete_ok.do' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
        <input type='hidden' name='seq' value='<%=seq%>'>  <%-- seq를 히든으로 보냄 --%>
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>제목</font> :
            </td>
            <td>
                <input type='text' name='subject' value='<%=subject %>' size='80' class='form' readonly>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<input type='password' name='password' size='10' maxlength='10' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>

        <table width='100%' cellpadding='0' cellspacing='0' border='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='./images/btn_del.gif' border='0'>&nbsp;
                <a href='./list.do'><img src='./images/btn_list.gif' border='0'></a>&nbsp;
                <a href='javascript:history.back();'><img src='./images/btn_view.gif' border='0'></a>
            </td>
        </tr>
        </table>
        </form>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


11. DeleteOkAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class DeleteOkAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 클래스를 생성, 클라이언트 정보 넣기
  BoardTO to = new BoardTO();
  to.setSeq(request.getParameter("seq"));
  to.setPassword(request.getParameter("password"));
     
  //DAO클래스에서 delete_ok에서 필요한 DB데이터를 처리해서 리턴
  BoardDAO dao = new BoardDAO();
     int flag = dao.boardDeleteOk(to);
     
     //데이터 넘김
     request.setAttribute("flag", flag);
  
 }

}

board_delete1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>

<%
       //action에서 처리한 최소한의 데이터 받음
    int flag = (Integer)request.getAttribute("flag");
    
    //성공실패 메시지
    out.println("<script type='text/javascript'>");
    if(flag ==0){
        out.println("alert('글삭제에 성공했습니다')");
        out.println("location.href='./list.do';");
    }else if(flag==1){
        out.println("alert('비밀번호가 잘못되었습니다.')");
        out.println("history.back();");
    }else{
        out.println("alert('글삭제에 실패했습니다')");
        out.println("history.back();");
    }
    out.println("</script>");
    
%>









12. pagelist가 있는 게시판 model2
폴더 구성


13.
context.xml
<?xml version="1.0" encoding="utf-8" ?>
<Context>
    <Resource 
        name="jdbc/oracle" 
        auth="Container" 
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver" 
        url="jdbc:oracle:thin:@localhost:1521:orcl"
        username="project" 
        password="project" 
        maxTotal="20" 
        maxWaitMillis="5000" />    
</Context>

BoardTO
package model1;

public class BoardTO {
 private String seq;
 private String subject;
 private String writer;
 private String mail;
 private String password;
 private String content;
 private String hit;
 private String wip;
 private String wdate;
 private int wgap;
 
 public String getSeq() {
  return seq;
 }
 public void setSeq(String seq) {
  this.seq = seq;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public String getWriter() {
  return writer;
 }
 public void setWriter(String writer) {
  this.writer = writer;
 }
 public String getMail() {
  return mail;
 }
 public void setMail(String mail) {
  this.mail = mail;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
 public String getHit() {
  return hit;
 }
 public void setHit(String hit) {
  this.hit = hit;
 }
 public String getWip() {
  return wip;
 }
 public void setWip(String wip) {
  this.wip = wip;
 }
 public String getWdate() {
  return wdate;
 }
 public void setWdate(String wdate) {
  this.wdate = wdate;
 }
 public int getWgap() {
  return wgap;
 }
 public void setWgap(int wgap) {
  this.wgap = wgap;
 }
}

BoardListTO
package model1;

import java.util.ArrayList;

public class BoardListTO {
    private int cpage;
    private int recordPerPage;
    private int blockPerPage;
    private int totalPage;
    private int startBlock;
    private int endBlock;
    private ArrayList<BoardTO> boardList;
    
    public BoardListTO() {
        this.cpage = 1;
        this.recordPerPage = 10;
        this.blockPerPage = 5;
        this.totalPage = 1;
    }
    
    public int getCpage() {
        return cpage;
    }
    public void setCpage(int cpage) {
        this.cpage = cpage;
    }
    public int getRecordPerPage() {
        return recordPerPage;
    }
    public void setRecordPerPage(int recordPerPage) {
        this.recordPerPage = recordPerPage;
    }
    public int getBlockPerPage() {
        return blockPerPage;
    }
    public void setBlockPerPage(int blockPerPage) {
        this.blockPerPage = blockPerPage;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getStartBlock() {
        return startBlock;
    }
    public void setStartBlock(int startBlock) {
        this.startBlock = startBlock;
    }
    public int getEndBlock() {
        return endBlock;
    }
    public void setEndBlock(int endBlock) {
        this.endBlock = endBlock;
    }
    public ArrayList<BoardTO> getBoardList() {
        return boardList;
    }
    public void setBoardList(ArrayList<BoardTO> boardList) {
        this.boardList = boardList;
    }
}

BoardDAO
package model1;

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

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

public class BoardDAO {
    private DataSource dataSource = null;

    public BoardDAO() {
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context)initCtx.lookup("java:comp/env");
            dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            System.out.println("에러 : " + e.getMessage());
        }
    }
    
    public void boardWrite() {    
    }

    public int boardWriteOk(BoardTO to) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        int flag = 1;
        
        try {
            conn = dataSource.getConnection();
            
            String sql = "insert into board1 values (board_seq.nextval, ?, ?, ?, ?, ?, 0, ?, sysdate)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSubject());
            pstmt.setString(2, to.getWriter());
            pstmt.setString(3, to.getMail());
            pstmt.setString(4, to.getPassword());
            pstmt.setString(5, to.getContent());
            pstmt.setString(6, to.getWip());
            
            if(pstmt.executeUpdate() == 1) {
                flag = 0;
            }
        } catch(SQLException e) {
            System.out.println("에러 : " + e.getMessage());
        } finally {
            if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
            if(conn != null) try { conn.close(); } catch(SQLException e) {}
        }
        
        return flag;
    }

    public BoardListTO boardList(BoardListTO listTO) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        int cpage = listTO.getCpage();
        int recordPerPage = listTO.getRecordPerPage();
        int blockPerPage = listTO.getBlockPerPage();
        
        try {
            
            conn = dataSource.getConnection();
            
            String sql = "select seq, subject, writer, to_char(wdate, 'YY.MM.DD') wdate, hit, trunc(sysdate-wdate) wgap from board1 order by seq desc";
            pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            
            rs = pstmt.executeQuery();
            
            rs.last();
            int totalRecord = rs.getRow();
            rs.beforeFirst();
            
            listTO.setTotalPage((totalRecord > 0 && totalRecord % recordPerPage == 0) ? (totalRecord / recordPerPage) : (totalRecord / recordPerPage) + 1); 
            
            int skip = (cpage - 1) * recordPerPage;
            if(skip != 0) rs.absolute(skip);
            
            ArrayList<BoardTO> boardList = new ArrayList<BoardTO>();
            
            for(int i=0 ; i<recordPerPage && rs.next() ; i++) {
                BoardTO to = new BoardTO();
                to.setSeq(rs.getString("seq"));
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setWdate(rs.getString("wdate"));
                to.setHit(rs.getString("hit"));
                to.setWgap(rs.getInt("wgap"));
                
                boardList.add(to);
            }
            
            listTO.setBoardList(boardList);
            
            listTO.setStartBlock(((cpage - 1) / blockPerPage) * blockPerPage + 1);
            listTO.setEndBlock(((cpage - 1) / blockPerPage) * blockPerPage + blockPerPage);
            if(listTO.getEndBlock() >= listTO.getTotalPage()) {
                listTO.setEndBlock(listTO.getTotalPage());
            }
            
        } catch(SQLException e) {
            System.out.println("에러 : " + e.getMessage());
        } finally {
            if(rs != null) try { rs.close(); } catch(SQLException e) {}
            if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
            if(conn != null) try { conn.close(); } catch(SQLException e) {}            
        }
        
        return listTO;
    }

    public BoardTO boardView(BoardTO to) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = dataSource.getConnection();
            
            String sql = "update board1 set hit=hit+1 where seq=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            
            pstmt.executeUpdate();
            pstmt.close();
            
            sql = "select subject, writer, mail, wip, wdate, hit, content from board1 where seq = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            
            rs = pstmt.executeQuery();
            if(rs.next()) {
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setMail(rs.getString("mail") == null ? "" : rs.getString("mail"));
                to.setWip(rs.getString("wip"));
                to.setWdate(rs.getString("wdate"));
                to.setHit(rs.getString("hit"));
                to.setContent(rs.getString("content") == null ? "" : rs.getString("content").replaceAll("\n", "<br />"));
            }
        } catch(SQLException e) {
            System.out.println("에러 : " + e.getMessage());
        } finally {
            if(rs != null) try { rs.close(); } catch(SQLException e) {}
            if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
            if(conn != null) try { conn.close(); } catch(SQLException e) {}
        }
        return to;
    }

    public BoardTO boardModify(BoardTO to) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = dataSource.getConnection();
            
            String sql = "select subject, writer, mail, content from board1 where seq = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            
            rs = pstmt.executeQuery();
            if(rs.next()) {
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setMail(rs.getString("mail") == null ? "" : rs.getString("mail"));
                to.setContent(rs.getString("content") == null ? "" : rs.getString("content"));
            }
        } catch(SQLException e) {
            System.out.println("에러 : " + e.getMessage());
        } finally {
            if(rs != null) try { rs.close(); } catch(SQLException e) {}
            if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
            if(conn != null) try { conn.close(); } catch(SQLException e) {}
        }
        return to;
    }

    public int boardModifyOk(BoardTO to) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        int flag = 2;
        try {
            conn = dataSource.getConnection();
            
            String sql = "update board1 set subject=?, mail=?, content=? where seq=? and password=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSubject());
            pstmt.setString(2, to.getMail());
            pstmt.setString(3, to.getContent());
            pstmt.setString(4, to.getSeq());
            pstmt.setString(5, to.getPassword());

            int result = pstmt.executeUpdate();
            if(result == 0) {
                flag = 1;
            } else if(result == 1) {
                flag = 0;
            }
        } catch(SQLException e) {
            System.out.println("에러 : " + e.getMessage());
        } finally {
            if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
            if(conn != null) try { conn.close(); } catch(SQLException e) {}
        }
        return flag;
    }

    public BoardTO boardDelete(BoardTO to) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = dataSource.getConnection();
            
            String sql = "select subject, writer, mail from board1 where seq = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            
            rs = pstmt.executeQuery();
            if(rs.next()) {
                to.setSubject(rs.getString("subject"));
                to.setWriter(rs.getString("writer"));
                to.setMail(rs.getString("mail") == null ? "" : rs.getString("mail"));
            }
        } catch(SQLException e) {
            System.out.println("에러 : " + e.getMessage());
        } finally {
            if(rs != null) try { rs.close(); } catch(SQLException e) {}
            if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
            if(conn != null) try { conn.close(); } catch(SQLException e) {}
        }
        return to;
    }

    public int boardDeleteOk(BoardTO to) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        int flag = 2;
        try {
            conn = dataSource.getConnection();
            
            String sql = "delete from board1 where seq=? and password=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, to.getSeq());
            pstmt.setString(2, to.getPassword());

            int result = pstmt.executeUpdate();
            if(result == 0) {
                flag = 1;
            } else if(result == 1) {
                flag = 0;
            }
        } catch(SQLException e) {
            System.out.println("에러 : " + e.getMessage());
        } finally {
            if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
            if(conn != null) try { conn.close(); } catch(SQLException e) {}
        }
        return flag;
    }
}


controller
package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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 model2.Action;
import model2.DeleteAction;
import model2.DeleteOkAction;
import model2.ListAction;
import model2.ModifyAction;
import model2.ModifyOkAction;
import model2.ViewAction;
import model2.WriteAction;
import model2.WriteOkAction;

/**
 * Servlet implementation class BoardController
 */
@WebServlet("*.do")
public class BoardController extends HttpServlet {
 private static final long serialVersionUID = 1L;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doProcess(request, response);
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doProcess(request, response);
 }
 
 protected void doProcess(HttpServletRequest request, HttpServletResponse response) {
  try {
   request.setCharacterEncoding("utf-8");
   
   String path = request.getRequestURI().replaceAll(request.getContextPath(), "");
   String url = "";
   Action action = null;
   
   //가상의 URL주소가 요청되면 Action처리후, 접속할 실제 폴더 위치 정함
   if(path.equals("/*.do") || path.equals("/list.do")){
    action = new ListAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_list1.jsp";
   }else if(path.equals("/view.do")){
    action = new ViewAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_view1.jsp";
   }else if(path.equals("/write.do")){
    action = new WriteAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_write1.jsp";
   }else if(path.equals("/write_ok.do")){
    action = new WriteOkAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_write1_ok.jsp";
   }else if(path.equals("/modify.do")){
    action = new ModifyAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_modify1.jsp";
   }else if(path.equals("/modify_ok.do")){
    action = new ModifyOkAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_modify1_ok.jsp";
   }else if(path.equals("/delete.do")){
    action = new DeleteAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_delete1.jsp";
   }else if(path.equals("/delete_ok.do")){
    action = new DeleteOkAction();
    action.execute(request, response);
    
    url="/WEB-INF/model2/board_delete1_ok.jsp";
   }
   
   //실제 URL주소 내용을 가상 URL에 띄움
   if(!url.equals("")){
    RequestDispatcher dispatcher = request.getRequestDispatcher(url);
    dispatcher.forward(request, response);
   }
  } catch (UnsupportedEncodingException e) {
   System.out.println("에러 : " +e.getMessage());
  } catch (ServletException e) {
   System.out.println("에러 : " +e.getMessage());
  } catch (IOException e) {
   System.out.println("에러 : " +e.getMessage());
  }
 }

}


14
WriteAction
package model2;

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

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class WriteAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //
  String cpage = request.getParameter("cpage");
  
  //데이터 넘김
  request.setAttribute("cpage", cpage);
 }

}

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

<%
    String cpage = (String)request.getAttribute("cpage");
%>

<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
<script type="text/javascript">
    function ChkForm(form) {
        if(form.value.trim() == "") {
            alert("제목을 입력하셔야 합니다.");
            return false;
        }
        if(form.writer.value.trim() == "") {
            alert("작성자를 입력하셔야 합니다.");
            return false;
        }
        if(form.password.value.trim() == "") {
            alert("비밀번호를 입력하셔야 합니다.");
            return false;
        }
    }
</script>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <form action='./write_ok.do' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>제목</font> :
            </td>
            <td>
                <input type='text' name='subject' size='80' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' size='10' maxlength='10' class='form'>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' size='40' maxlength='70' class='form'>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<input type='password' name='password' size='10' maxlength='10' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>내용</font> :
            </td>
            <td>
                <textarea name='content' style='width:620;height:300' class='form'></textarea>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>

        <table width='100%' cellpadding='0' cellspacing='0' border='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='./images/btn_wri.gif' border='0'>&nbsp;
                <a href='./list.do?cpage=<%=cpage %>'><img src='./images/btn_list.gif' border='0'></a>
            </td>
        </tr>
        </table>
        </form>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


15
WriteOkAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class WriteOkAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 입력
  BoardTO to = new BoardTO();
  to.setSubject(request.getParameter("subject"));
  to.setWriter(request.getParameter("writer"));
  to.setMail(request.getParameter("mail"));
  to.setPassword(request.getParameter("password"));
  to.setContent(request.getParameter("content"));
  to.setWip(request.getRemoteAddr());
  //데이터 처리
  BoardDAO dao = new BoardDAO();
  int flag = dao.boardWriteOk(to);
  
  //데이터 넘김
  request.setAttribute("flag", flag);
 }

}

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

<%
    //action에서 데이터 받음
    int flag = (Integer)request.getAttribute("flag");
    
    out.println("<script type='text/javascript'>");
    if(flag == 0) {
        out.println("alert('글쓰기에 성공했습니다.');");
        out.println("location.href='./list.do';");
    } else {
        out.println("alert('글쓰기에 실패했습니다.');");
        out.println("history.back();");
    }
    out.println("</script>");
%>


16
ListAction
package model2;

import java.util.ArrayList;

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

import model1.BoardDAO;
import model1.BoardListTO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ListAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 받음
  int cpage = 1;
  if(request.getParameter("cpage") != null && !request.getParameter("cpage").equals("")) {
   cpage = Integer.parseInt(request.getParameter("cpage"));
  }

  BoardListTO listTO = new BoardListTO();
  listTO.setCpage(cpage);
  
  //데이터 처리
  BoardDAO dao = new BoardDAO();
  listTO = dao.boardList(listTO);
  
  //데이터 넘김
  request.setAttribute("listTO", listTO);
 }

}

board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="model1.BoardTO" %>    
<%@ page import="model1.BoardListTO" %>
<%@ page import="java.util.ArrayList" %>

<%
    //action에서 데이터 받음
    BoardListTO listTO = (BoardListTO)request.getAttribute("listTO");
    
    ArrayList<BoardTO> boardList = listTO.getBoardList();
    int cpage =listTO.getCpage();
    int startBlock = listTO.getStartBlock();
    int endBlock = listTO.getEndBlock();
    int totalPage = listTO.getTotalPage();
    int blockPerPage = listTO.getBlockPerPage();
    
    StringBuffer result = new StringBuffer();
        
    for(BoardTO to : boardList) {
        String seq = to.getSeq();
        String subject = to.getSubject();
        String writer = to.getWriter();
        String wdate = to.getWdate();
        String hit = to.getHit();
        int wgap = to.getWgap();

        result.append("<table width='100%' border='0' cellpadding='0' cellspacing='0'>");
        result.append("<tr>");
        result.append("    <td height='1'></td>");
        result.append("</tr>");
        result.append("<tr>");
        result.append("    <td>");
        result.append("        <table width='100%' border='0' cellpadding='0' cellspacing='0'>");
        result.append("        <tr height='25' onMouseOver=\"this.className='evencell'\" onMouseOut=\"this.className=''\">");
        result.append("            <td width='40' align='center'>" + seq + "</td>");
        result.append("            <td>");
        result.append("                <span style='width:370' class='elltxt'>");
        result.append("                    <a href='./view.do?cpage=" + cpage + "&seq=" + seq + "'>" + subject + "</a>");
        if(wgap == 0) {
            result.append("                <img src='./images/ico_n.gif' width='8' height='8' border='0' hspace='3'>");
        }
        result.append("                </span>");
        result.append("            </td>");
        result.append("            <td width='100' align='center'>" + writer + "</td>");
        result.append("            <td width='80' align='center'>" + wdate + "</td>");
        result.append("            <td width='50' align='center'>" + hit + "</td>");
        result.append("        </tr>");
        result.append("        </table>");
        result.append("    </td>");
        result.append("</tr>");
        result.append("<tr>");
        result.append("    <td height='1'></td>");
        result.append("</tr>");
        result.append("<tr>");
        result.append("    <td align='center' class='imgline'></td>");
        result.append("</tr>");
        result.append("</table>");    
    }    
%>

<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td align='center' class='gline'></td>
        </tr>
        <tr>
            <td align='center'>
                <table width='100%' border='0' cellpadding='0' cellspacing='0' class='titlecell'>
                <tr height='25' align='center'>
                    <td width='40'>No</td>
                    <td>제목</td>
                    <td width='100'>이름</td>
                    <td width='80'>등록일</td>
                    <td width='50'>조회수</td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align='center' class='gline'></td>
        </tr>
        <tr>
            <td height='3'></td>
        </tr>
        </table>

<%=result %>    

        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='500' height='30'>
<!-- 페이지 네비게이션 -->   
<%
    if(startBlock == 1) {
        out.println("<img src='./images/moveset/p_left_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'>");
    } else {
        out.println("<a href='./list.do?cpage=" + (startBlock - blockPerPage) + "'><img src='./images/moveset/p_left_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'></a>");
    }
    
    // 이전 페이지 가기
    if(cpage == 1) {
        out.println("<img src='./images/moveset/p_left_arrow_02_off.gif' align='absmiddle' hspace='8' border='0'>");    
    } else {
        out.println("<a href='./list.do?cpage=" + (cpage - 1) + "'><img src='./images/moveset/p_left_arrow_02.gif' align='absmiddle' hspace='8' border='0'></a>");    
    }
     
    out.println("<img src='./images/moveset/p_orange.gif' align='absmiddle' hspace='3' border='0'>");

     // 현재 페이지 표시
     for(int i=startBlock ; i<=endBlock ; i++) {
          if (cpage == i) {
               out.println("<b><u>[" + i + "]</u></b>&nbsp;");
          } else {
               out.println("<a href='./list.do?cpage=" + i + "'>" + i + "</a>&nbsp;");
          }
     }
     
     out.println("<img src='./images/moveset/p_orange.gif' align='absmiddle' hspace='3' border='0'>");
     
     if(cpage == totalPage) {
          out.println("<img src='./images/moveset/p_right_arrow_02_off.gif' align='absmiddle' hspace='8' border='0'>");
     } else {
          out.println("<a href='./list.do?cpage=" + (cpage + 1) + "'><img src='./images/moveset/p_right_arrow_02.gif' align='absmiddle' hspace='8' border='0'></a>");
     }

     if(endBlock == totalPage) {
         out.println("<img src='./images/moveset/p_right_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'>");
     } else {
        out.println("<a href='./list.do?cpage=" + (startBlock + blockPerPage) + "'><img src='./images/moveset/p_right_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'></a>");
     }
%>
                
        
            </td>
            <td align='right'>
                <a href='./write.do?cpage=<%=cpage %>'><img src='./images/btn_wri.gif' border='0'></a>
            </td>
        </tr>
        </table>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


17
ViewAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ViewAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 받음
  BoardTO to = new BoardTO();
  String cpage = request.getParameter("cpage");
  to.setSeq(request.getParameter("seq"));
  
  //데이터 처리
  BoardDAO dao = new BoardDAO();
  to = dao.boardView(to);
  
  //데이터 넘김
  request.setAttribute("cpage", cpage);
  request.setAttribute("to", to);
 }

}

board_view1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="model1.BoardTO" %>    

<%
    //action 데이터 받음
    String cpage = (String)request.getAttribute("cpage");    
    BoardTO to = (BoardTO)request.getAttribute("to");
    
    String seq = to.getSeq();
    String subject = to.getSubject();
    String writer = to.getWriter();
    String mail = to.getMail();
    String wip = to.getWip();
    String wdate = to.getWdate();
    String hit = to.getHit();
    String content = to.getContent();
%>

<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
        <tr>
            <td height='25'><font class='titdot'>&#149;&nbsp;</font><font class='title'>제목</font> | <%=subject %></td>
        </tr>
        <tr>
            <td class='imgline'></td>
        </tr>
        <tr>
            <td height='25'><font class='titdot'>&#149;&nbsp;</font><font class='title'>이름</font> | <%=writer %>(<%=mail %>) (<%=wip %>)</td>
        </tr>
        <tr>
            <td class='imgline'></td>
        </tr>
        <tr>
            <td height='25'>
                <table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
                <tr>
                    <td><font class='titdot'>&#149;</font><font class='title'> 날짜</font> | <%=wdate %></td>
                    <td align='right'><font class='titdot'>&#149;&nbsp;</font><font class='title'>조회</font> | <%=hit %></td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td  class='imgline'></td>
        </tr>
        <tr>
            <td height='100'><%=content %></td>
        </tr>
        </table>

        <table width='100%' border='0' cellspacing='0' cellpadding='0' align='center'>
        <tr>
            <td id='tailarea'></td>
        </tr>
        </table>

        <table width='100%' border='0' cellspacing='1' cellpadding='1'>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        <tr>
            <td width='500' height='30'>
                <a href='./write.do?cpage=<%=cpage %>'><img src='./images/btn_wri.gif' border='0'></a>                  
                <a href='./modify.do?cpage=<%=cpage %>&seq=<%=seq%>'><img src='./images/btn_mod.gif' border='0'></a>
                <a href='./delete.do?cpage=<%=cpage %>&seq=<%=seq%>'><img src='./images/btn_del.gif' border='0'></a>
            </td>
            <td align='right'>
                <a href='list.do?cpage=<%=cpage %>'><img src='./images/btn_list.gif' border='0'></a>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


18
ModifyAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ModifyAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 받음
  BoardTO to = new BoardTO();
  String cpage = request.getParameter("cpage");
  to.setSeq(request.getParameter("seq"));
  
  //데이터 처리
  BoardDAO dao = new BoardDAO();
  to = dao.boardModify(to);
  
  //데이터 넘김
  request.setAttribute("to", to);
  request.setAttribute("cpage", cpage);
 }

}

board_modify1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="model1.BoardTO" %>    

<%
    //Action 데이터 받음
    String cpage = (String)request.getAttribute("cpage");    
    BoardTO to = (BoardTO)request.getAttribute("to");

    String seq = to.getSeq();
    String subject = to.getSubject();
    String writer = to.getWriter();
    String mail = to.getMail();
    String content = to.getContent();
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
<script type="text/javascript">
    function ChkForm(form) {
        if(form.subject.value.trim() == "") {
            alert("제목을 입력하셔야 합니다");
            return false;
        }
            
        if(form.password.value.trim() == "") {
            alert("비밀번호를 입력하셔야 합니다");
            return false;
        }
    }
</script>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <form action='./modify_ok.do' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
        <input type='hidden' name='cpage' value='<%=cpage %>' />
        <input type='hidden' name='seq' value='<%=seq %>' />
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>제목</font> :
            </td>
            <td>
                <input type='text' name='subject' value='<%=subject %>' size='80' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form'>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<input type='password' name='password' size='10' maxlength='10' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>내용</font> :
            </td>
            <td>
                <textarea name='content' style='width:620;height:300' class='form'><%=content %></textarea>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>

        <table width='100%' cellpadding='0' cellspacing='0' border='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='./images/btn_mod.gif' border='0'>&nbsp;
                <a href='./list.do?cpage=<%=cpage %>'><img src='./images/btn_list.gif' border='0'></a>&nbsp;
                <a href='javascript:history.back();'><img src='./images/btn_view.gif' border='0'></a>
            </td>
        </tr>
        </table>
        </form>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


19
ModifyOkAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class ModifyOkAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 받음
  BoardTO to = new BoardTO();
  String cpage = request.getParameter("cpage");

  to.setSeq(request.getParameter("seq"));
  to.setSubject(request.getParameter("subject"));
  to.setMail(request.getParameter("mail"));
  to.setPassword(request.getParameter("password"));
  to.setContent(request.getParameter("content"));
  
  //데이터 처리
  BoardDAO dao = new BoardDAO();
  int flag = dao.boardModifyOk(to);
  
  //데이터 넘김
  request.setAttribute("to", to);
  request.setAttribute("cpage", cpage);
  request.setAttribute("flag", flag);
 }

}

board_modify1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="model1.BoardTO" %>    

<%
    //Action 데이터 받음
    BoardTO to = (BoardTO)request.getAttribute("to");
    String cpage = (String)request.getAttribute("cpage");    
    int flag = (Integer)request.getAttribute("flag");
    String seq = to.getSeq();
    
    out.println("<script type='text/javascript'>");
    if(flag == 0) {
        out.println("alert('글수정에 성공했습니다.');");
        out.println("location.href='./view.do?cpage=" + cpage + "&seq=" + seq + "';");
    } else if(flag == 1) {
        out.println("alert('비밀번호가 잘못되었습니다.');");
        out.println("history.back();");
    } else {
        out.println("alert('글수정에 실패했습니다.');");
        out.println("history.back();");
    }
    out.println("</script>");
%>


20
DeleteAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class DeleteAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 받음
  BoardTO to = new BoardTO();
  String cpage = request.getParameter("cpage");
  String seq = request.getParameter("seq");
  to.setSeq(request.getParameter("seq"));
  
  //데이터 처리
  BoardDAO dao = new BoardDAO();
  to = dao.boardDelete(to);
  
  //데이터 넘김
  request.setAttribute("cpage", cpage);
  request.setAttribute("to", to);
 }

}

board_delete1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="model1.BoardTO" %>    

<%
    //Action 데이터 받음
    String cpage = (String)request.getAttribute("cpage");    
    BoardTO to = (BoardTO)request.getAttribute("to");
    
    String seq = to.getSeq();
    String subject = to.getSubject();
    String writer = to.getWriter();
    String mail = to.getMail();
%>

<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='./images/common.css'>
<script type="text/javascript">
    function ChkForm(form) {
        if(document.wfrm.password.value.trim() == "") {
            alert('비밀번호를 입력하세요.');
            return false;
        }
    }
</script>
</head>

<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>

<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
    <td bgcolor='#ffffff' style='padding:20'>
        <form action='./delete_ok.do' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
        <input type='hidden' name='seq' value='<%=seq %>' />
        <table width='100%' border='0' cellpadding='0' cellspacing='0'>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>제목</font> :
            </td>
            <td>
                <input type='text' name='subject' value='<%=subject %>' size='80' class='form' readonly>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='imgline'></td>
        </tr>
        <tr>
            <td width='70' style='padding:5' valign='top' align='right'>
                <font class='titdot'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<input type='password' name='password' size='10' maxlength='10' class='form'>
            </td>
        </tr>
        <tr>
            <td colspan='2' class='gline'></td>
        </tr>
        </table>

        <table width='100%' cellpadding='0' cellspacing='0' border='0'>
        <tr>
            <td width='500' height='30'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='./images/btn_del.gif' border='0'>&nbsp;
                <a href='./list.do?cpage=<%=cpage %>'><img src='./images/btn_list.gif' border='0'></a>&nbsp;
                <a href='javascript:history.back();'><img src='./images/btn_view.gif' border='0'></a>
            </td>
        </tr>
        </table>
        </form>
    </td>
</tr>
</table>        
        
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
    <td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>

</body>
</html>


21
DeleteOkAction
package model2;

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

import model1.BoardDAO;
import model1.BoardTO;

//MVC model에서 모든데이터 처리하고 view에 필요한 최소한의 데이터를 넘김
public class DeleteOkAction implements Action {

 @Override
 public void execute(HttpServletRequest request, HttpServletResponse response) {
  //데이터 받음
  BoardTO to = new BoardTO();
  to.setSeq(request.getParameter("seq"));
  to.setPassword(request.getParameter("password"));
  
  //데이터 처리
  BoardDAO dao = new BoardDAO();
  int flag = dao.boardDeleteOk(to);
  
  //데이터 넘김
  request.setAttribute("flag", flag);
  
 }

}

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

<%
    //Action데이터 받음
    int flag = (Integer)request.getAttribute("flag");
    
    out.println("<script type='text/javascript'>");
    if(flag == 0) {
        out.println("alert('글삭제에 성공했습니다.');");
        out.println("location.href='./list.do';");
    } else if(flag == 1) {
        out.println("alert('비밀번호가 잘못되었습니다.');");
        out.println("history.back();");
    } else {
        out.println("alert('글삭제에 실패했습니다.');");
        out.println("history.back();");
    }
    out.println("</script>");
%>


댓글 없음:

댓글 쓰기