SMALL
안녕하세요, 코린이의 코딩 학습기 채니 입니다.
개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다.
@실습문제 - 게시글 등록
Controller
BoardEnrollServlet
@WebServlet("/board/boardEnroll")
public class BoardEnrollServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private BoardService boardService = new BoardService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/views/board/boardEnroll.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 1. 사용자 입력 값 처리
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String content = request.getParameter("content");
Board board = new Board(0, title, writer, content, 0, null);
// 2. 업무로직
int result = boardService.insertBoard(board);
// 3. redirect 처리
request.getSession().setAttribute("msg", "게시글을 성공적으로 등록하였습니다.");
response.sendRedirect(request.getContextPath() + "/board/boardList");
} catch(Exception e) {
e.printStackTrace();
throw e;
}
}
}
Service
BoardService
public int insertBoard(Board board) {
Connection conn = getConnection();
int result = 0;
try {
result = boardDao.insertBoard(conn, board);
commit(conn);
} catch(Exception e) {
rollback(conn);
throw e;
} finally {
close(conn);
}
return result;
}
Dao
BoardDao
// insertBoard = insert into board (no, title, writer, content) values(seq_board_no.nextval, ?, ?, ?)
public int insertBoard(Connection conn, Board board) {
PreparedStatement pstmt = null;
int result = 0;
String sql = prop.getProperty("insertBoard");
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, board.getTitle());
pstmt.setString(2, board.getWriter());
pstmt.setString(3, board.getContent());
result = pstmt.executeUpdate();
} catch (SQLException e) {
throw new BoardException("게시글 등록 오류!", e);
} finally {
close(pstmt);
}
return result;
}
boardEnroll.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/common/header.jsp" %>
<% %>
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/board.css" />
<section id="board-container">
<h2>게시판 작성</h2>
<form
name="boardEnrollFrm"
action="<%=request.getContextPath() %>/board/boardEnroll"
method="post">
<table id="tbl-board-view">
<tr>
<th>제 목</th>
<td><input type="text" name="title" required></td>
</tr>
<tr>
<th>작성자</th>
<td>
<input type="text" name="writer" value="<%= loginMember.getMemberId() %>" readonly/>
</td>
</tr>
<tr>
<th>첨부파일</th>
<td>
<input type="file" name="upFile1">
<input type="file" name="upFile2">
</td>
</tr>
<tr>
<th>내 용</th>
<td><textarea rows="5" cols="40" name="content"></textarea></td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="등록하기">
</th>
</tr>
</table>
</form>
</section>
<script>
/**
* boardEnrollFrm 유효성 검사
*/
document.boardEnrollFrm.onsubmit = (e) => {
const frm = e.target;
//제목을 작성하지 않은 경우 폼제출할 수 없음.
if(!/^.+/.test(frm.title.value)) {
alert("제목을 작성해주세요.");
frm.title.focus();
return false;
}
//내용을 작성하지 않은 경우 폼제출할 수 없음.
if(!/^(.|\n)+$/.test(frm.content.value)) {
alert("내용을 작성해주세요.");
frm.content.focus();
return false;
}
}
</script>
<%@ include file="/WEB-INF/views/common/footer.jsp" %>
LIST
'Java > └ [Servlet & JSP] Practice' 카테고리의 다른 글
[실습문제] 첨부파일 없는 게시글 수정 (0) | 2022.07.05 |
---|---|
[실습문제] 회원 정보 수정 (0) | 2022.06.26 |
[실습문제] 내 정보 보기 (0) | 2022.06.25 |