JSP/SERVLET
2018.03.30 / 18:31

JSTL을 이용한 문서관리시스템

봄이
추천 수 46

1.개요

(1)테이블 구조

role테이블 - rid , name

doc테이블 - did,title,owner,category,role,description,cabinet,date

cabinet테이블-cid,title,role,description

 

(2)sql파일

USE root;

CREATE TABLE `role` (
 `rid` TINYINT (3) UNSIGNED NOT NULL, 
 `name` VARCHAR (20) NOT NULL, 
 PRIMARY KEY(`rid`), UNIQUE(`rid`));

INSERT INTO role (rid, name) VALUES (5, '일반회원');
INSERT INTO role (rid, name) VALUES (4, '단골회원');
INSERT INTO role (rid, name) VALUES (3, '고급회원');
INSERT INTO role (rid, name) VALUES (2, '중간관리자');
INSERT INTO role (rid, name) VALUES (1, '시스템관리자');

CREATE TABLE `doc` (
 `did` INT (7) UNSIGNED NOT NULL AUTO_INCREMENT, 
 `title` VARCHAR (50) NOT NULL,
 `owner` VARCHAR (10) NOT NULL,
 `category` VARCHAR (30), 
 `role` TINYINT (3) UNSIGNED NOT NULL, 
 `description` TEXT, 
 `cabinet` TINYINT (3) UNSIGNED NOT NULL, 
 `data` VARCHAR (50) NOT NULL, 
 PRIMARY KEY(`did`), UNIQUE(`did`), INDEX(`did`)) ;

CREATE TABLE `cabinet` (
 `cid` TINYINT (3) UNSIGNED NOT NULL AUTO_INCREMENT, 
 `title` VARCHAR (50) NOT NULL, 
 `role` TINYINT (3) UNSIGNED NOT NULL, 
 `description` TINYTEXT, 
 PRIMARY KEY(`cid`), UNIQUE(`cid`), INDEX(`cid`));

 

2.캐비넷 관련소스

 

//cabinet.jsp

//파라미터에 따라 적절한 페이지로 이동하게 한다.

 

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${uid == null}">
 <c:redirect url="/login.jsp"/> //세션부존재시 로그인페이지로 이동 
</c:if>

<c:choose>
 <c:when test="${roleNo <= 2}">                                       //사용자권한 2이하이면 
  <c:set var="isManager" value="yes" scope="session" />
 </c:when>
 <c:otherwise>
  <c:set var="isManager" value="no" scope="session" />
 </c:otherwise>
</c:choose>

<sql:setDataSource var="ds
driver="com.mysql.jdbc.Driver" 
user="root"  password="mysql"
scope="session"
url="jdbc:mysql://localhost/root?useUnicode=true&characterEncoding=euckr"/>

<c:choose>

//파라미터에 따라 이동할 페이지 선택 
 <%-- 캐비넷 등록 --%>           
 <c:when test="${param.cmd == 'add'}">
  <jsp:forward page="cabinet_add.jsp"/>
 </c:when>
 
 <%-- 변경된 캐비넷 정보를 DB에 저장 --%>
 <c:when test="${param.cmd == 'change'}">
  <jsp:forward page="cabinet_change.jsp"/>
 </c:when>
 
 <%-- 캐비넷 변경을 위한 내용 보기 --%>
 <c:when test="${param.cmd == 'get'}">
  <jsp:forward page="cabinet_get.jsp"/>
 </c:when>

 <%-- 캐비넷 삭제 --%>
 <c:when test="${param.cmd == 'delete'}">
  <jsp:forward page="cabinet_delete.jsp"/>
 </c:when>
 
 <%-- 캐비넷 목록 보기(캐비넷 정보 변경을 위해) --%> 
 <c:when test="${param.cmd == 'modify'}">
  <jsp:forward page="cabinet_list.jsp"/>
 </c:when>
 
 <%-- 캐비넷 목록 보기 --%>  //디폴트UI페이지
 <c:otherwise>
  <jsp:forward page="cabinet_list.jsp"/>
 </c:otherwise>
</c:choose>

 

//cabinet_list.jsp -화면에 캐비넷 목록을 뿌린다.기본 UI페이지

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<c:set var="isModify" value="no"/>
<c:if test="${isManager== 'yes' and param.cmd =='modify' }">
 <c:set var="isModify" value="yes"/>
</c:if>

<sql:query dataSource="${ds}" var="rs">
 select * from cabinet 
</sql:query>  
<html><head><title>캐비넷 목록</title>
<link type="text/css" rel="stylesheet" href="/mystyle.css">
</head><body><h3 id='header'>캐비넷 목록</h3>
<div id='menu'>
 <%@ include file="edms_menu.jsp" %>
</div>

<div id='main'>
<table border=1 width=100%>
 <tr><th>이름</th><th>설명</th><th>읽기권한</th>
 <c:if test="${isModify == 'yes'}"><th>삭제</th></c:if></tr>
 <c:forEach var="row" items="${rs.rows}">
 <tr><td>
  <c:choose> //변경가능여부에 따른 문서의 선택적 링크걸기
   <c:when test="${isModify == 'yes'}">
   <a href=cabinet.jsp?cmd=get&id=<c:out value="${row.cid}"/>>
   </c:when>
   <c:otherwise>
   <a href=doc_manager.jsp?cabinet=<c:out value="${row.cid}"/>>
   </c:otherwise>
  </c:choose>  

  <c:out value="${row.title}"/></a></td>
  <td><c:out value="${row.description}"/></td>     
  <td><select name=role>
  <c:choose>
   <c:when test="${row.role == 1}">
    <option value='1'> 시스템관리자
   </c:when>
   <c:when test="${row.role == 2}">
    <option value='2'> 중간관리자
   </c:when>
   <c:when test="${row.role == 3}">
    <option value='3'> 고급회원
   </c:when>
   <c:when test="${row.role == 4}">
    <option value='4'> 단골회원
   </c:when>
   <c:when test="${row.role == 5}">
    <option value='5'> 일반회원
   </c:when>
  </c:choose>
  </select> </td> 
  <c:if test="${isModify == 'yes'}"> //삭제버튼에 대한 링크걸기 
   <td><form method=post action=cabinet.jsp>   
    <input type=hidden name=cmd value='delete'>
    <input type=hidden name=cid value='${row.cid}'>
    <input type=submit value='삭제'>
   </form></td>
  </c:if>
  </tr>
 </c:forEach>
</table>
</div>
</body>
</html>    

 

//edms_menu.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div id="menucontainer">
 <div id="shatop"></div>
 <h3 class="center">메 뉴</h3>
 <div class="blank">&nbsp;</div>
 <a href=/login.jsp>메인페이지</a>
 <div class="blank">&nbsp;</div>  
<c:if test="${roleNo <= 2}">
 <a href=cabinet_input.jsp>캐비넷 등록</a>
 <div class="blank">&nbsp;</div>
 <a href=cabinet.jsp?cmd=modify>캐비넷 관리</a>
 <div class="blank">&nbsp;</div>
</c:if> 
 <a href=cabinet.jsp>캐비넷 보기</a>  
 <div class="blank">&nbsp;</div>
 <a href=doc_input.jsp>문서 등록</a>
 <div class="blank">&nbsp;</div>
 <a href=doc_keyword.jsp>문서 찾기</a>
 <div class="blank">&nbsp;</div> 
 <div id="shabottom"></div>  
</div>

 

//cabinet_input.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${ds == null or isManager == 'no'}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<html><head><title>캐비넷 등록</title>
<link type="text/css" rel="stylesheet" href="/mystyle.css">
</head>
<body>
<h3 id='header'>캐비넷 등록</h3>
<div id='menu'>
 <%@ include file="edms_menu.jsp" %>
</div>
<div id='main'>
<form action=cabinet.jsp  method=post>
 <table>
  <tr>
   <td>캐비넷 이름</td>
   <td><input type=text name=title></td>
  </tr>
  <tr>
   <td>캐비넷 설명</td>
   <td><textarea name=description 
    rows=10 cols=35></textarea></td>
  </tr>
  <tr>
   <td>읽기 권한</td>
   <td><select name=role>
   <option value="1" >시스템관리자
   <option value="2" >중간관리자
   <option value="3" >고급회원
   <option value="4" >단골회원
   <option selected value="5" >일반회원
   </select></td>
  </tr>
  <tr><td colspan=2><center>
   <input type=submit value="등록">
   <input type=reset value="취소">
   </center></td>
  </tr>
 </table>
 <input type=hidden name=cmd value="add">
</form>
</div>
</body>
</html>


 

//cabinet_add.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:transaction dataSource="${ds}">
 <sql:update>
  INSERT INTO cabinet VALUES( ?, ?, ?)
  <sql:param value="${param.title}"/>
  <sql:param value="${param.role}"/>
  <sql:param value="${param.description}"/>
 </sql:update>
 <sql:query var="rs">
  SELECT cid FROM cabinet WHERE cid 
  = (SELECT MAX(cid) FROM cabinet)
 </sql:query>
 <c:forEach var="row" items="${rs.rows}">
  <c:set var="cn" value="${row.cid}"/>
 </c:forEach>
</sql:transaction>

<%  // 문서 저장을 위한 디렉토리 생성
 String path = application.getRealPath("/edms");
 path += "/" + (Integer)pageContext.getAttribute("cn");
 myclasses.FileUtil.mkdir(path);
%>
<c:redirect url="cabinet.jsp"/>

 

//cabinet_get.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:query dataSource="${ds}" var="rs">
 select * from cabinet where cid=?
 <sql:param value="${param.id}"/>
</sql:query>
<html><head><title>캐비넷 데이터 변경</title>
<link type="text/css" rel="stylesheet" href="/mystyle.css">
</head><body>
<h3 id='header'>캐비넷 데이터 변경 </h3>
<div id='menu'>
 <%@ include file="edms_menu.jsp" %>
</div>
<div id='main'>
<form method=post action=cabinet.jsp>
<table>
<c:forEach var="row" items="${rs.rows}">
 <tr><td>캐비넷 이름</td>
  <td><input type=text name=title 
  value='<c:out value="${row.title}"/>'></td></tr>
 <tr><td>문서 설명</td>
  <td><textarea name=description rows=10 
  cols=35><c:out value="${row.description}"/></textarea></td></tr>
 <tr><td>읽기권한</td>
  <td><select name=role>
   <option value="1" 
   <c:if test="${row.role == 1}">selected</c:if>>시스템관리자
   <option value="2" 
    <c:if test="${row.role == 2}"> selected</c:if>>중간관리자
   <option value="3" 
    <c:if test="${row.role == 3}"> selected</c:if>>고급회원
   <option value="4" 
    <c:if test="${row.role == 4}"> selected</c:if>>단골회원
   <option value="5" 
    <c:if test="${row.role == 5}"> selected</c:if>>일반회원
   </select>
  </td></tr>
</c:forEach>
</table>
 <input type=hidden name=id value='<c:out value="${param.id}"/>'>
 <input type=hidden name=cmd value='change'>
 <input type=submit value="변경하기">
</form>
</div>
</body>
</html>

 

 

//cabinet_change.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:transaction dataSource="${ds}">
 <sql:update>
  UPDATE cabinet  SET  title =?, role = ?, 
   description = ? where cid = ?
  <sql:param value="${param.title}"/>
  <sql:param value="${param.role}"/>
  <sql:param value="${param.description}"/>  
  <sql:param value="${param.id}"/>
 </sql:update> 
</sql:transaction>
<c:redirect url="cabinet.jsp"/>

 

//cabinet_delete.jsp

<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<c:if test="${ds == null or isManager == 'no'}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:transaction dataSource="${ds}">
 <sql:update>
  delete from cabinet where cid = ?
  <sql:param value="${param.cid}"/>
 </sql:update>
 <sql:update>
  delete from doc where cabinet = ?
  <sql:param value="${param.cid}"/>
 </sql:update> 
</sql:transaction>

<%  // 문서 저장을 위한 디렉토리 삭제
 String path = application.getRealPath("/edms");
 path += "/" + request.getParameter("cid");
 myclasses.FileUtil.rmdir(new java.io.File(path));
%>
<c:redirect url="cabinet.jsp"/>

 

3.문서 관련 소스

//doc_manager.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<%-- 문서 정보를 DB에 저장 --%>
<c:choose>
 <c:when test="${param.cmd == 'add'}">
  <jsp:forward page="doc_add.jsp"/>
 </c:when>
 
<%-- 변경된 문서 정보를 DB에 저장 --%> 
 <c:when test="${param.cmd == 'change'}">
  <jsp:forward page="doc_change.jsp"/>
 </c:when>

<%-- 문서 삭제 --%> 
 <c:when test="${param.cmd == 'delete'}">
  <jsp:forward page="doc_delete.jsp"/>
 </c:when>
 
<%-- 문서 정보 변경을 위한 내용 보기 --%> 
 <c:when test="${param.cmd == 'get'}">
  <jsp:forward page="doc_get.jsp"/>
 </c:when>

<%-- 검색 결과 및 캐비넷 문서 목록 보기 --%> //디폴트UI페이지
 <c:otherwise>
  <jsp:forward page="doc_list.jsp"/>
 </c:otherwise>
</c:choose>

 

 

//doc_get.jsp -문서를 가져와서 문서내용을 출력한다.

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<c:choose>
 <%-- 키워드로 문서 찾기 SQL --%>
 <c:when test="${param.cmd == 'search'}">   
  <sql:query dataSource="${ds}" var="rs"> 
   select * from doc where category like ? or 
   description like ?
   <sql:param value="%${param.keyword}%"/>
   <sql:param value="%${param.keyword}%"/>
  </sql:query>
 </c:when>
  
 <%-- 캐비넷의 문서 목록 보기 SQL --%>  
 <c:otherwise>
  <sql:query dataSource="${ds}" var="rs">
  select * from doc where cabinet = ?
   <sql:param value="${param.cabinet}"/>
  </sql:query>  
 </c:otherwise>
</c:choose>
  
<%-- SQL로 찾은 내용 출력 --%>
<% String dir = request.getContextPath() + "/edms/";
 pageContext.setAttribute("dir", dir); %>
 
<html><head><title>캐비넷 내용</title></head>
<link type="text/css" rel="stylesheet" href="/mystyle.css">
<body><h3 id='header'>캐비넷 내용</h3>
<div id='menu'>
 <%@ include file='edms_menu.jsp' %>
</div>
<div id='main'>
<table border=1 width=100%>
 <tr><th>문서명</th><th>카테고리</th>
 <th>설명 </th><th>캐비넷</th><th>수정</th>
 <c:if test="${roleNo <= 2}"><th>삭제</th></c:if></tr>
 <c:forEach var="row" items="${rs.rows}">
 <c:if test="${roleNo <= row.role}">
  <tr><td>
  <c:if test="${row.data != ''}">
  <a href='/servlet/DownloadFile?file=<c:out 
  value="${row.data}"/>&cabinet=<c:out value="${row.cabinet}"/>'>
  </c:if>
  <c:out value="${row.title}"/></a></td>
  <td><c:out value="${row.category}"/></td>
  <td><c:out value="${row.description}"/></td>
  <td><a href=doc_manager.jsp?cabinet=<c:out                          //캐비넷리스트페이지로 이동
   value="${row.cabinet}"/>>
   <c:out value="${row.cabinet}"/></a></td>
  <td> &nbsp;
  <c:if test="${ row.owner == uid or row.role > roleNo}">
  <a href=doc_manager.jsp?cmd=get&id=<c:out value= "${row.did}" />>  //특성문서가져와서 뿌리는 페이지
   변경</a> </c:if></td>
  <c:if test="${roleNo <= 2}"> <td>
   <a href=doc_manager.jsp?cmd=delete&did=<c:out 
      value="${row.did}"/>>삭제</a></td>
  </c:if></tr>
 </c:if>
 </c:forEach>
 </table>
</div>
</body>
</html>    

 

//doc_input.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:query dataSource="${ds}" var="rs">
 select  cid, title  from cabinet 
</sql:query>  
  
<html><head><title>문서 등록</title></head>
<link type="text/css" rel="stylesheet" href="/mystyle.css">
<body><h3 id='header'>문서 등록</h3>
<div id='menu'>
 <%@ include file="edms_menu.jsp" %>
</div>
<div id='main'>
<form action=doc_upload.jsp method=post enctype="multipart/form-data">
<table> 
 <tr>
  <td>문서 제목</td>
  <td> <input type=text name=title></td>
 </tr>
 <tr>
  <td>카테고리</td>
  <td><input type=text name=category></td>
 </tr>
 <tr>
  <td>읽기 권한</td>
  <td> <select name=role>
  <option value="1" >시스템관리자
  <option value="2" >중간관리자
  <option value="3" >고급회원
  <option value="4" >단골회원
  <option selected value="5" >일반회원
  </select> 
  </td>
 </tr>
 <tr>
  <td>문서 설명</td>
  <td>
  <textarea name=description rows=10 cols=35></textarea></td>  //문서내용 입력하는 부분 
 </tr>
 <tr>
  <td>캐비넷</td>
  <td> <select name=cabinet>
  <c:forEach var="row" items="${rs.rows}">
    <option value=<c:out value="${row.cid}"/>>
    <c:out value="${row.title}"/>
  </c:forEach>
  </select>
  </td>
 </tr>
 <tr>
  <td>문서 파일</td>
  <td><input type=file name=data></td>
 </tr>
 <tr>
  <td colspan=2>
  <input type=submit value="등록">
  <input type=reset value="취소">
  </td>
 </tr>
</table>
<input type=hidden name=cmd value="add">
</form>
</div>
</body>
</html>

//doc_upload.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page import="java.util.*" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page errorPage="error.jsp"%>
<% 
 ServletContext cxt = getServletContext();
 String dir = cxt.getRealPath("/edms/");
 int max = 5 * 1024 * 1024; // 5MB
 MultipartRequest multi = 
  new MultipartRequest(request, dir, max, "KSC5601");
 Enumeration params = multi.getParameterNames();
 while(params.hasMoreElements()) {
  String name = (String) params.nextElement();
  String value = multi.getParameter(name);
  request.setAttribute(name, value); //request객체에 값들을 저장한다.
 }


 Enumeration files = multi.getFileNames();
 String fileName = null;
 request.setAttribute("data", fileName); 
 while (files.hasMoreElements()) {
  String name = (String)files.nextElement();
  fileName = multi.getFilesystemName(name);
  request.setAttribute("data", fileName);//파일이름을 저장한다.
  break;
 }
 String cmd = multi.getParameter("cmd");
 String gotoPage = "/doc_manager.jsp?cmd=change";
 if(fileName != null) {
  String cabinet = multi.getParameter("cabinet");
  String from = dir + "/" + fileName;
  String newName = fileName;
  if(cmd != null && cmd.equals("add")) {
   int index = fileName.lastIndexOf(".");
   if(index != -1) {
    String bareName = fileName.substring(0, index);
    String ext = fileName.substring(index+1, 
     fileName.length());
    long num = new Date().getTime();
    newName = bareName + "_" + num + "." + ext; //중복방지를 위해서 시간까지 더해서 보낸다.
    request.setAttribute("data", newName);
    gotoPage = "/doc_manager.jsp?cmd=add";    
   }
  } 
  String to = dir + "/" + cabinet + "/" + newName;
  myclasses.FileUtil.move(from, to);
  } else if(fileName == null) {
  if(cmd != null && cmd.equals("add")) {
   gotoPage = "/doc_manager.jsp?cmd=add";
  }
 }
 RequestDispatcher rd = 
  cxt.getRequestDispatcher(gotoPage);
 rd.forward(request, response);  
%>

 

//doc_add.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:transaction dataSource="${ds}">
 <sql:update>
  INSERT INTO doc VALUES( ?, ?, ?, ?, ?, ?, ?)
  <sql:param value="${requestScope.title}"/>
  <sql:param value="${uid}"/>
  <sql:param value="${requestScope.category}"/>    
  <sql:param value="${requestScope.role}"/>
  <sql:param value="${requestScope.description}"/>
  <sql:param value="${requestScope.cabinet}"/>
  <c:choose>
   <c:when test="${requestScope.data == null}">
    <sql:param value=""/>
   </c:when>
   <c:otherwise>
    <sql:param value="${requestScope.data}"/>
   </c:otherwise>
  </c:choose>
 </sql:update> 
</sql:transaction>
<c:redirect url="cabinet.jsp"/>

 

//DownloadFile.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownloadFile extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res) 
  throws IOException, ServletException {
  req.setCharacterEncoding("KSC5601");   
  byte [] buffer = new byte[1024];
  String file = req.getParameter("file");
  String cabinet = req.getParameter("cabinet");
  String hfile = myclasses.StringUtil.toHangul(file); //한글 문제
  res.setContentType("application/octet");//파일다운로드형태
  res.setHeader("Content-Disposition", //저장할 파일이름을 지정하기위해
   "attachment;filename=" + file);

  ServletOutputStream out = res.getOutputStream();
  try {
   ServletContext cxt = getServletConfig().getServletContext();
   String path =  "/edms/" + cabinet + "/" + hfile;
   path = cxt.getRealPath(path);
   BufferedInputStream in = new 
    BufferedInputStream(new FileInputStream(path));
   int n = 0;
   while((n=in.read(buffer, 0, 1024)) != -1) {
    out.write(buffer, 0, n); 
   }
   out.close();
   in.close();
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
}

 

//doc_get.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:query dataSource="${ds}" var="rs">
 select * from doc where did=?
 <sql:param value="${param.id}"/>
</sql:query>
  
<sql:query dataSource="${ds}" var="rs2">
 select  cid, title  from cabinet 
</sql:query>
    
<html><head><title>문서 정보  변경</title></head>
<link type="text/css" rel="stylesheet" href="/mystyle.css">
<body> <h3 id='header'>문서 정보 변경 </h3>
<div id='menu'>
 <%@ include file="edms_menu.jsp" %>
</div>  
<div id='main'>
 <form method=post action=doc_upload.jsp 
  enctype="multipart/form-data"> 
 <table>
 <c:forEach var="row" items="${rs.rows}">
  <tr><td>문서 이름</td>
  <td> <input type=text name=title 
   value='<c:out value="${row.title}"/>'> </td></tr>
  <tr><td>문서 카테고리</td>
  <td><input type=text name=category 
   value='<c:out value="${row.category}"/>'></td></tr>     
  <tr><td>문서 설명</td>
  <td><textarea name=description rows=10 
       cols=35><c:out value="${row.description}"/></textarea>
  </td></tr>     
  <tr><td>읽기 권한</td>
  <td><select name=role>
   <option value="1" 
   <c:if test="${row.role == 1}">selected</c:if>>시스템관리자
   <option value="2" 
   <c:if test="${row.role == 2}"> selected</c:if>>중간관리자
   <option value="3" 
   <c:if test="${row.role == 3}"> selected</c:if>>고급회원
   <option value="4" 
   <c:if test="${row.role == 4}"> selected</c:if>>단골회원
   <option value="5" 
   <c:if test="${row.role == 5}"> selected</c:if>>일반회원
   </select>
  </td></tr>
  <tr><td>캐비넷</td>
  <td> <select name=cabinet>
   <c:forEach var="row2" items="${rs2.rows}">
    <option value=<c:out value="${row2.cid}"/>
    <c:if test="${row.cabinet == row2.cid}">selected
    </c:if>><c:out value="${row2.title}"/>    
   </c:forEach>
   <input type=hidden name=cn 
    value='<c:out value="${row.cabinet}"/>'>
   <input type=hidden name=fname 
    value='<c:out value="${row.data}"/>'> </tr>
  <tr><td>파일 업데이트</td>
  <td><input type=file name=data></td></tr>    
 </c:forEach>
 </table>
 <input type=hidden name='id'
   value='<c:out value="${param.id}"/>'>
 <input type=hidden name='cmd' value='change'>
 <input type=submit value="변경하기">
</form>
</div>
</body>
</html>

 

//doc_change.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<fmt:requestEncoding value="KSC5601"/>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:transaction dataSource="${ds}">
 <sql:update>
  UPDATE  doc  SET title =?, owner = ?, category=?,  role = ?, 
   description = ?, cabinet=?, data = ?  where did = ?
  <sql:param value="${requestScope.title}"/>
  <sql:param value="${uid}"/>
  <sql:param value="${requestScope.category}"/>
  <sql:param value="${requestScope.role}"/>
  <sql:param value="${requestScope.description}"/>
  <sql:param value="${requestScope.cabinet}"/>
  <c:choose>
   <c:when test="${requestScope.data == null}">
    <sql:param value="${requestScope.fname}"/>
   </c:when>
   <c:otherwise>
    <sql:param value="${requestScope.data}"/>
   </c:otherwise>
  </c:choose>
  <sql:param value="${requestScope.id}"/>
 </sql:update> 
</sql:transaction>
<c:if test="${param.cabinet != param.cn}">
<%
 String fname = request.getParameter("fname");
 String newFileName = request.getParameter("data");
 String cn = request.getParameter("cn");
 String from = application.getRealPath("/edms/" + cn + "/" + fname); 
 if(fname != null && fname.equals(newFileName)) { 
  String newcn = request.getParameter("cabinet");
  String to = application.getRealPath("/edms/" + 
   newcn + "/" + fname);
  myclasses.FileUtil.move(from, to);   
 } else if(fname != null && !fname.equals(newFileName)) {
  myclasses.FileUtil.delete(from); 
 }
%> 
</c:if>
<c:redirect url="cabinet.jsp"/>

 

//doc_delete.jsp

<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<c:if test="${ds == null or isManager == 'no'}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<sql:query dataSource="${ds}" var="rs">
 select cabinet, data from doc where did=?
 <sql:param value="${param.did}"/>
</sql:query>
<c:forEach var="row" items="${rs.rows}">
 <c:set var="cn" value="${row.cabinet}"/>
 <c:set var="fname" value="${row.data}"/>
</c:forEach>
<%  // 저장된 파일 삭제
 String path = application.getRealPath("/edms");
 Integer cn = (Integer)pageContext.getAttribute("cn");
 String fname = (String)pageContext.getAttribute("fname");
 path += "/" + cn + "/" + fname;
 myclasses.FileUtil.delete(path);
%>
<%-- doc 테이블 레코드 삭제 --%>
<sql:transaction dataSource="${ds}">
 <sql:update>
  delete from doc where did = ?
  <sql:param value="${param.did}"/>
  </sql:update>
</sql:transaction>  
<c:redirect url="cabinet.jsp"/>

 

//doc_keyword.jsp

<%@ page contentType="text/html;charset=KSC5601" %>
<%@ page errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${ds == null}">
 <c:redirect url="cabinet.jsp"/>
</c:if>

<html><head><title>문서 찾기</title></head>
<link type="text/css" rel="stylesheet" href="/mystyle.css">
<body>
<h3 id='header'>문서 찾기</h3>
<div id='menu'>
 <%@ include file="edms_menu.jsp" %>
</div>
<div id='main'>
<form action=doc_manager.jsp  method=post>
<p> 키워드 <input type=text name=keyword>
     <input type=submit value="찾기">
<input type=hidden name=cmd value=search>
</form>
</div>
</body>
</html>