게시글 작성을 위한 실습
public void boardInsert(String title, String content, String writer) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ds.getConnection();
String sql = "INSERT INTO boardTbl (title, content, writer) VALUES (?, ?, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setString(3, writer);
System.out.println(title);
System.out.println(writer);
System.out.println(content);
System.out.println(sql);
pstmt.executeUpdate();
System.out.println("리스트 업데이트 완료");
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
pstmt.close();
} catch(Exception e) {
e.printStackTrace();
}
}
} // boardInsert() END.
BoardDAO에 작성한 boardInsert() DAO의 전문입니다.
pstmt와 connection으로 SQL 쿼리문을 전송합니다.
이 메서드는 title, content, writer 변수를 요구합니다.
/boardInsertForm 서블릿
@WebServlet("/boardInsertForm")
public class BoardInsertForm extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BoardInsertForm() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dp = request.getRequestDispatcher("/board/boardInsertForm.jsp");
dp.forward(request, response);
}
}
엄밀히 말해 전달데이터가 없으므로(바인딩을 하지 않음) 리다이렉트로 처리해도 기능적인 차이는 없습니다.
리다이렉트로 처리할 경우 파일명이나 폴더구조가 일부 노출될 수 있어 포워딩을 했습니다.
/boardInsert servlet
@WebServlet("/boardInsert")
public class BoardInsert extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BoardInsert() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String content = request.getParameter("content");
BoardDAO dao = BoardDAO.getInstance();
dao.boardInsert(title, content, writer);
response.sendRedirect("http://localhost:8181/MyFirstWeb/boardList");
}
이제 모든 준비가 끝났습니다.
/boardList에서
a태그로 간단하게 form 서블릿으로 보내주고
form에서 /boardInsert로 post방식으로 전송하는
구조가 완성입니다.
이제 화면에 보여줄 jsp파일만 만들면 됩니다.
<body>
<div class="container-lg">
<div class="col-sm-12">
<form action="http://localhost:8181/MyFirstWeb/boardInsert" method="post">
<div class="col-sm-9">
<br/>
<br/>
<label for="title1" class="form-label">제목</label>
<input type="text" name="title" class="form-control" id="title1" placeholder="제목을 적어주세요.">
<br/>
</div>
<div class="col-sm-3">
<label for="writer1" class="form-label">글쓴이</label>
<input type="text" name="writer" class="form-control" id="writer1" placeholder="글쓴이를 적어주세요.">
<br/>
</div>
<div class="mb-12">
<label for="textarea1" class="form-label"> 본 문 </label>
<textarea class="form-control" name="content" id="textarea1" cols="40" rows="20"></textarea>
<br/>
</div>
<br/>
<input type="submit" value="확인" class="btn btn-success btn-mb-3">
</form>
</div>
</div>
</body>
일단은 여기까지입니다.