본문 바로가기

기존카테고리/JSP

공지사항 - 파일 쓰기, 읽기

 notice_main.jsp


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

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript">

function readNotice(){

window.open("notice/notice_read.jsp", "notice","width=250,height=300");

}

</script>

</head>

<body>


<!-- 공지읽기, 공지쓰기 메뉴 -->

<a href="javascript:readNotice()">공지 읽기</a>

<a href="notice/notice_write.jsp">공지 쓰기</a>


</body>

</html>


------------------------------------------------------------------------------------------------------------


notice/notice_write.jsp


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

    pageEncoding="UTF-8" import="java.util.*"%>

    

<%

Calendar cal = Calendar.getInstance();

int yy = cal.get(Calendar.YEAR);

int mm = cal.get(Calendar.MONTH)+1;

int dd = cal.get(Calendar.DAY_OF_MONTH);

out.println("<font color=red><b>");

out.println(yy+"년"+mm+"월"+dd+"일");

out.println("</b></font>");

String fname = yy+"";

if(mm<10)

fname+=("0"+mm);

else

fname+=mm;

if(dd<10)

fname+=("0"+dd);

else

fname+=dd;

fname+=".html";

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>


<h2>오늘의 공지사항</h2>

<form action="notice_save.jsp" method="post">

<table border=1 width=500>

<tr>

<th>제목</th>

<td>

<input type="text" name="title"/>

</td>

</tr>

<tr>

<th>공지내용</th>

<td>

<textarea name="info" rows="5" cols="50"></textarea> 

</td>

</tr>

<tr>

<th>파일명</th>

<td>

<input type="text" name="filename" value="<%=fname%>" disabled>

<input type="hidden" name="filename" value="<%=fname%>">

</td>

</tr>

<tr>

<td colspan=2 align="center">

<input type="submit" value="작성" />

<input type="reset" value="취소" />

</td>

</tr>

</table>


</form>



</body>

</html>



---------------------------------------------------------------------------------


notice/notice_save.jsp


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

    pageEncoding="UTF-8" import="java.io.*"%>

<!-- save 처리과정 -->

<% request.setCharacterEncoding("UTF-8"); %>

<%

/*

1. 사용자가 입력한 글제목, 내용, 파일명을 얻어오는 과정

2. 파일명과 FileWriter와 스트림 연결

3. 스트림을 통해서 글제목과 내용을 파일에 쓴다.

4. 스트림 닫기

5. 파일에 쓰기가 완료 되었으면 메시지 띄우기

*/

request.setCharacterEncoding("euc-kr");

String title = request.getParameter("title");

String content = request.getParameter("info");

String fileName = request.getParameter("filename");




if(fileName == null ||fileName.trim().equals("")){

response.sendRedirect("notice_write.jsp");

return;

}


if(title==null || content==null || title.trim().equals("")||content.trim().equals("")){

out.println("공지 내용과 제목을 입력하세요!!!");

out.println("<a href=notice_write.jsp>돌아가기</a>");

return;

}


// 파일 절대 경로 구하기

// request 객체에 getRealPath("/notice");

// config 내장 객체에 getServletContext() 메소드

// ServletContext ctx = config.getServletContext();


ServletContext ctx = config.getServletContext();

String allPath = ctx.getRealPath("/notice");


//String allPath = request.getRealPath("/notice");

out.println("공지사항 저장될 경로:"+allPath);



String allFname = allPath+"/"+fileName;

FileWriter fw = null;

PrintWriter pw = null;


try{

fw = new FileWriter(allFname);

pw = new PrintWriter(fw, true);

pw.println("<body>");

pw.println("<meta charset=UTF-8/>");

pw.println("<b>"+title+"</b><br/>");

pw.println(content);

pw.println("</body>");

pw.close();

fw.close();

}catch(IOException e){

out.println("입출력오류: "+e.getMessage());

return;

}



%>

<script>

alert("<%=fileName%> 파일 쓰기 완료");

location.href="<%=request.getContextPath()%>/notice_main.jsp";

</script>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>


</body>

</html>


-----------------------------------------------------------------------------------------------------


notice/notice_read.jsp


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

    pageEncoding="UTF-8" import="java.util.*, java.io.*"%>

    

<%

/*

1. 공지파일을 읽기 위해서 오늘 날짜를 구한다.

2. 날짜.html --> 파일명을 만든다.

3. 파일명과 파일을 읽는 노드스트림을 연결

4. 필터링

5. 스트림을 통해서 읽어오기

6. 읽어온 내용을 브라우저에 출력하기

7. 스트림 close 처리

*/

Calendar cal=Calendar.getInstance();

int yy = cal.get(Calendar.YEAR);

int mm = cal.get(Calendar.MONTH)+1;

int dd = cal.get(Calendar.DAY_OF_MONTH);

out.println(yy+"년"+mm+"월"+dd+"일");

String fname =yy+"";

if(mm<10) fname+=("0"+mm);

else fname+=mm;

if(dd<10) fname+=("0"+dd);

else fname+=dd;

fname+=".html";

//out.println(fname);

String rFile = config.getServletContext().getRealPath("/notice");

rFile+="/"+fname;

//out.println("읽어올 파일 경로:"+rFile);

FileReader fr = null;

BufferedReader br = null;

try{

fr = new FileReader(rFile);

br = new BufferedReader(fr);

String contents = "";

while((contents=br.readLine())!= null){

out.println(contents+"<br/>");

}

br.close(); fr.close();

}catch(FileNotFoundException e){

out.println("오늘은 공지사항이 없습니다!!!!");

out.println("<a href='javascript:self.close();'");

out.println("close </a>");

}catch(IOException e){

out.println("IO오류:"+e.getMessage());

}

%>    

    

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>


</body>

</html>


---------------------------------------------------

'기존카테고리 > JSP' 카테고리의 다른 글

Command 패턴  (0) 2017.06.21
FrontController 패턴  (0) 2017.06.15
JSTL  (0) 2017.06.13
EL(Expression Language) 2  (0) 2017.06.13
EL(Expression Language)  (0) 2017.06.13