⑤ 글 작성 jsp 만들기
c<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>글작성</h3>
<form action = "/webApp3/guest/add" method = "post">
<table border = "1">
<tr><th>작성자</th><td><input type = "text" name = "writer"></td></tr>
<tr><th>비밀번호</th><td><input type = "text" name = "pwd"></td></tr>
<tr><th>내용<th><textarea rows = "5" cols= "40" name = "content"></textarea></tr>
<tr><th>등록</th><td><input type = "submit" value = "등록"></td></tr>
</table>
</form>
</body>
</html>
해당 table 을 보여주고 글 작성 form을 통해 입력 받은 값들을 post 방식으로 servlet 에 받아와서 db에 저장하게 구현한다.
⑥ "/guest/add" servlet 구현하기
package guestbook.controller;
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 guestbook.GuestBookService;
import guestbook.GuestBookVo;
/**
* Servlet implementation class add
*/
@WebServlet("/guest/add")
public class add extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public add() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("euc-kr");
response.setCharacterEncoding("euc-kr");
response.setContentType("text/html; charset = EUC-KR");
RequestDispatcher dis = request.getRequestDispatcher("/guestbook/add.jsp");
dis.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("euc-kr");
response.setCharacterEncoding("euc-kr");
response.setContentType("text/html; charset = EUC-KR");
String writer = request.getParameter("writer");
int pwd = Integer.parseInt(request.getParameter("pwd"));
String content = request.getParameter("content");
GuestBookService service = new GuestBookService();
service.addBook(new GuestBookVo(0,writer,pwd, content,null,0));
response.sendRedirect("/webApp3/guest/list");
}
}
'MVC > jsp & servlet' 카테고리의 다른 글
[jsp와 servlet] 익명 방명록 만들기 ⑤ 작성자로 검색하기 만들기 (0) | 2023.03.29 |
---|---|
[jsp와 servlet] 익명 방명록 만들기 ④ 글 목록 만들기 (0) | 2023.03.29 |
[jsp와 servlet] 익명 방명록 만들기 ② view 메인 페이지 만들기 (0) | 2023.03.29 |
[jsp와 servlet] 익명 방명록 만들기 ① Model 생성 (0) | 2023.03.29 |
[jsp와 servlet] jsp와 servlet을 이용한 회원 탈퇴 (0) | 2023.03.27 |