본문 바로가기
Backend

09월 04일 수 | JSP 09 - JSP 미니 프로젝트 Ⅰ- 글쓰기 (DB연결)

by 구라미 2019. 9. 4.

 

디렉토리 구조

 

 

글작성 구현하기

CRUD중 Create, Read가 필요한 기능이다.

 

 

1. BbsForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../header.jsp"%>
<!-- 본문 시작 -->

<div class="container-fluid">
<h2>WRITE</h2>
<form name="bbsfrm" method="post" action="bbsIns.jsp"
	onsubmit="return bbsCheck(this)">

	<table class="table">
		<tr>
			<th>작성자</th>
			<td><input type="text" name="wname" size="10" maxlength="20"
				required class="form-control"></td>
		</tr>
		<tr>
			<th>제목</th>
			<td><input type="text" name="subject" size="30" maxlength="100"
				required class="form-control"></td>
		</tr>
		<tr>
			<th>내용</th>
			<td><textarea rows="10" cols="30" name="content"
					class="form-control"></textarea></td>
		</tr>
		<tr>
			<th>비밀번호</th>
			<td><input type="password" name="passwd" size="10"
				maxlength="10" class="form-control"></td>
		</tr>
		<tr>
			<td colspan="2">
			<input type="submit" value="쓰기" class="btn btn-primary">
			<input type="reset"  value="취소" class="btn btn-outline-secondary">
			<span style="margin-left:20px; float:right;">
				<a href="bbsList.jsp" class="btn btn-default">글목록</a>
			</span>
			</td>
		</tr>
	</table>

</form>
</div>
<!-- 본문 끝 -->
<%@ include file="../footer.jsp"%>

 

2. BbsIns.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.bbs.*" %>
<%@ include file="ssi.jsp" %>
<%@ include file="../header.jsp"%>
<!-- 본문 시작 bbsIns.jsp-->
<%
	//1) 사용자가 입력한 정보를 변수에 담기
	String wname  =request.getParameter("wname").trim();
	String subject=request.getParameter("subject").trim();
	String content=request.getParameter("content").trim();
	String passwd =request.getParameter("passwd").trim();
	String ip     =request.getRemoteAddr();//요청pc의 ip
	
	//2) 1)의 내용을 dto객체에 담기
	dto.setWname(wname);
	dto.setSubject(subject);
	dto.setContent(content);
	dto.setPasswd(passwd);
	dto.setIp(ip);
	
	//3) 2)의 내용을 tb_bbs테이블에 추가하기
	int cnt=dao.insert(dto);
	
	//4) 글쓰기 성공 후 bbsList.jsp로 이동하기
	if(cnt==0){
		out.println("<p>글 입력에 실패했습니다</p>");
		out.println("<p><a href='javascript:history.back()'>[다시시도]</a></p>");
	}else{
		out.println("<script>");
		out.println("	alert('글이 입력되었습니다.');");
		out.println(" location.href='bbsList.jsp?col="+col+"&word="+word+"&nowPage"+nowPage+"';");
		out.println("</script>");
	}//if end	
%>


<!-- 본문 끝 --> 
<%@ include file="../footer.jsp"%>

 

 

3. BbsRead.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="ssi.jsp"%>
<%@ include file="../header.jsp"%>
<!-- 본문 시작 -->
<div class="container-fluid">
<h2>READ</h2>

<%
 int bbsno = Integer.parseInt(request.getParameter("bbsno"));
 dto = dao.read(bbsno);
 if (dto == null) {
  out.print("해당글없음");
 } else {

	//조회수 증가
	dao.incrementCnt(bbsno);
%>
<div class = "table-div">
<table class="table">
	<tr>
		<th colspan="4" class ="readTitle"><%=dto.getSubject()%></th>
	</tr>
	<tr>
		<td>글번호 : <%=dto.getBbsno()%></td>
		<td>작성자 : <%=dto.getWname()%></td>
		<td>등록일 : <%=dto.getRegdt()%></td>
		<td>조회수 : <%=dto.getReadcnt()%></td>
	</tr>
	<tr>
		<td colspan="4" class="contentRead" style="padding:100px 0;">
		<%
			String content = dto.getContent();			
			content = content.replaceAll("'", "&apos;");
			content = content.replaceAll("\"", "&quot;");
			content = content.replaceAll("<", "&lt;");
			content = content.replaceAll(">", "&gt;");
			content = content.replaceAll("\r\n", "<br>");
			out.print(content);		
		%>
		
		</td>
	</tr>
	<tr>
		  <td colspan = "4" style="text-align: left; height: 50px;">
		  <form action="bbsList.jsp" 
		  		onsubmit="return commentCheck(this)">		  		  	
		  	<input type="text"   name="cwname" class="form-control" placeholder="작성자" style="display: unset; width:20%" >
		  	<textarea rows="3" cols="15" name="comment" placeholder="내용을 입력하세요."  class="form-control" style="display: unset; width:70%"></textarea>		  	
		  	<input type="submit" value="댓글달기" class="btn btn-default" style="width: 9%; height: 74px;">
		  </form>
		  </td>
	<tr>
		<td colspan ="2" style="text-align: left;">
			<a href="bbsReply.jsp?bbsno=<%=dto.getBbsno()%>&nowPage=<%=nowPage%>" class="reply btn btn-default">답변</a>
			<a href="bbsUpdate.jsp?bbsno=<%=dto.getBbsno()%>&col=<%=col%>&word=<%=word%>&nowPage=<%=nowPage%>" class="modify btn btn-default">수정</a> 
			<a href="bbsDelete.jsp?bbsno=<%=dto.getBbsno()%>&col=<%=col%>&word=<%=word%>&nowPage=<%=nowPage%>" class="delete btn btn-danger">삭제</a>					
		</td>		
		<td colspan ="2">
			<p style="float:right;">	
				<a href="bbsForm.jsp"  class="btn btn-primary">글쓰기</a>
				<a href="bbsList.jsp?col=<%=col%>&word=<%=word%>&nowPage=<%=nowPage%>"  class="btn btn-default">글목록</a>	
			</p>
		</td>
	</tr>
</table>

<%
	}//if end
%>

</div>
</div>
<!-- 본문 끝 -->
<%@ include file="../footer.jsp"%>

댓글