MVC패턴 게시판 만들기 - 글쓰기
BoardWriteFrom.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <body> <div style="text_align: center"> <h2>글 작성하기</h2> <form action="BoardWriteProcCon.do" method="post"> <table align="center" width="600" border="1" boardcolort="gray" bgcolor="skyblue"> <tr height="40"> <td width="150" align="center">작성자</td> <td width="450"> <input type="text" name="writer" size="60"> </td> </tr> <tr height="40"> <td width="150" align="center">제목</td> <td width="450"> <input type="text" name="subject" value="[답변]" size="60"></td> </tr> <tr height="40"> <td width="150" align="center">이메일</td> <td width="450"> <input type="email" name="email" size="60"> </td> </tr> <tr height="40"> <td width="150" align="center">비밀번호</td> <td width="450"> <input type="password" name="password" size="60"></td> </tr> <tr height="40"> <td width="150" align="center">글내용</td> <td width="450"> <textarea rows="10" cols="60" name="content"></textarea> </td> </tr> <!-- form에서 사용자로부터 입력받지 않고 데이터를 넘김 --> <tr> <td align="center" colspan="2"> <input type="submit" value="글 등록"> <input type="reset" value="다시 입력"> <input type="button" onclick="location.href='BoardList.jsp'" value="전체글보기"></td> </tr> </table> </form> </div> </body> </html> | cs |
BoardWriteProcCon.java(Controller)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package control; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.BoardBean; import model.BoardDAO; @WebServlet("/BoardWriteProcCon.do") public class BoardWriteProcCon extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { reqPro(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { reqPro(request, response); } protected void reqPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); BoardBean bean = new BoardBean(); //데이터 맵핑/ 자동맵핑해주는 usebean 사용 못하므로 bean.setWriter(request.getParameter("writer")); bean.setSubject(request.getParameter("subject")); bean.setEmail(request.getParameter("email")); bean.setPassword(request.getParameter("password")); bean.setContent(request.getParameter("content")); //데이터베이스 객체 생성 BoardDAO bdao = new BoardDAO(); bdao.WriteBoard(bean); RequestDispatcher rd = request.getRequestDispatcher("BoardListCon.do"); rd.forward(request, response); } } | cs |
BoardDAO.java (Model) 중 일부
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | //하나의 게시글을 DB에 저장하는 메서드 //데이터를 5개만 받았으므로 나머지 데이터도 초기화해서 넣어줘야한다. public void WriteBoard(BoardBean bean) { getCon(); //값들을 초기화해주고 ref에는 db에서 가장 큰값 +1을 넣는다 int ref = 0; int re_step=1; int re_level=1; try { //받아온 데이터를 업데이트하기 전에 //DB에서 가장 큰 ref값을 받아와 +1해서 로컬 ref에 담아준다. String refsql = "select max(ref) from board"; pstmt = con.prepareStatement(refsql); rs = pstmt.executeQuery(); if(rs.next()) { ref= rs.getInt(1)+1; } //데이터 삽입 String sql= "insert into board values(board_seq.NEXT_VAL,?,?,?,?,sysdate,?,?,?, 0,?)"; pstmt = con.prepareStatement(sql); pstmt.setString(1, bean.getWriter()); pstmt.setString(2, bean.getSubject()); pstmt.setString(3, bean.getEmail()); pstmt.setString(4, bean.getPassword()); pstmt.setInt(5, ref); pstmt.setInt(6, re_step); pstmt.setInt(7, re_level); pstmt.setString(8, bean.getContent()); pstmt.executeUpdate(); con.close(); }catch(Exception e) { e.printStackTrace(); } } | cs |
'JSP > JSP & Servlet ' 카테고리의 다른 글
MVC패턴 게시판 만들기 - 답글달기 (0) | 2019.05.26 |
---|---|
MVC패턴 게시판 만들기 - 글보기 (0) | 2019.05.24 |
MVC패턴 게시판 만들기 - 전체 게시글 보기 (0) | 2019.05.24 |
MVC패턴 게시판 (0) | 2019.05.23 |
ORA-00001: unique constraint (SYSTEM.MEMBER_PK) violated (0) | 2019.05.23 |