공지 읽기, 쓰기

2022. 12. 11. 20:42JSP

jsp_EX_3 프로젝트 내 WebContent 내 notice_main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta 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>

jsp_EX_3 프로젝트 내 WebContent 내 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 = fname + ("0"+mm);
	else fname =fname + mm;
	
	if(dd<10) fname =fname + ("0"+dd);//fname+=("0"+dd);
	else fname = fname + dd; //fname+=dd;
	
	fname = fname+".html";//fnmae += ".html";
	//out.println(fname);
	
	String rFile = config.getServletContext().getRealPath("/notice");
	rFile = rFile + "/" + fname; // 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

jsp_EX_3 프로젝트 내 WebContent 내 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 = fname + ("0"+mm); // fname += ("0"+mm);
	else
		fname = fname + mm; //fname+=mm;
	if(dd<10)
		fname = fname + ("0"+dd); // fname+=("0"+dd);
	else
		fname = fname + dd; // fname+=dd;
		
	fname = fname + ".html"; // fname += ".html";
%>
<!DOCTYPE html>
<html>
<head>
<meta 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>

jsp_EX_3 프로젝트 내 WebContent 내 notice 내 notice_save.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.io.*"%>
<!-- save 처리과정 -->
<%
	/*
	1. 사용자가 입력한 글제목, 내용, 파일명을 얻어오는 과정
	2. 파일명과 FileWriter와 스트림 연결
	3. 스트림을 통해서 글제목과 내용을 파일에 쓴다.
	4. 스트림 닫기
	5. 파일에 쓰기가 완료 되었으면 메시지 띄우기
	*/
	request.setCharacterEncoding("UTF-8");
	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);//autoflush기능스트림안에 있는내용이남아있지않고모두출력되게하는
		//전부연결.브라우저로 출력되는 내용을 filewriter와 같이 연결시켜줌.
		pw.println("<body>");
		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 type="text/javascript">
	alert("<%=fileName%> 파일에 쓰기 작업 완료!!!");
	location.href="<%=request.getContextPath()%>/notice_main.jsp";
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

'JSP' 카테고리의 다른 글

directive 태그  (0) 2022.12.13
계산기  (0) 2022.12.13
JSTL  (0) 2022.12.11
확장자 패턴  (0) 2022.12.11
Command 패턴  (0) 2022.12.11