- BoardDto 빈 만들기
- write.jsp 페이지 만들기
- mvc패턴 순서대로 데이터 전달
- dao에서 DB연결 쿼리문 생성
- 잘 들어갔나 확인
BoardDto 빈 만들기
package com.web.toy.model;
public class BoardDto {
private int no;
private String userId;
private String subject;
private String content;
private String rogtime;
public BoardDto() {
// TODO Auto-generated constructor stub
}
public BoardDto(int no, String userId, String subject, String content, String rogtime) {
super();
this.no = no;
this.userId = userId;
this.subject = subject;
this.content = content;
this.rogtime = rogtime;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getRogtime() {
return rogtime;
}
public void setRogtime(String rogtime) {
this.rogtime = rogtime;
}
@Override
public String toString() {
return "BoardDto [no=" + no + ", userId=" + userId + ", subject=" + subject + ", content=" + content
+ ", rogtime=" + rogtime + "]";
}
}
write.jsp 페이지 만들기
대강 부트스트랩에서 아무거나 복붙한다
mvc패턴을 만든다
view -> controller -> service -> dao ->db 순으로 위의 데이터를 전달 한다.
서블릿
private void write(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
HttpSession session = request.getSession();
Member member = (Member) session.getAttribute("userinfo");
String path="/main.jsp";
if(member != null) {
String subject = request.getParameter("subject");
String content = request.getParameter("content");
//board객체 삽입
BoardDto boardDto = new BoardDto();
boardDto.setUserId(member.getUserid());
boardDto.setSubject(subject);
boardDto.setContent(content);
BoardServiceImp.getBoardServiceImp().write(boardDto);
}else {
request.setAttribute("msg", "로그인을 해주세요");
path = "/index.jsp";
}
request.getRequestDispatcher(path).forward(request, response);
}
service
public void write(BoardDto boardDto) throws SQLException {
Connection conn = DBUtil.getConnection();
BoardDaoImp.getBoardDaoImp().write(conn, boardDto);
DBUtil.close(conn);
return;
}
boardDao의 write메소드를 생성
public void write(Connection conn, BoardDto boardDto) {
PreparedStatement ps = null;
try {
String sql = "insert into guestbook(userid, subject, content, regtime) values(?,?,?,now()); ";
ps = conn.prepareStatement(sql);
ps.setString(1, boardDto.getUserId());
ps.setString(2, boardDto.getContent());
ps.setString(3, boardDto.getSubject());
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}finally {
DBUtil.close(ps);
}
}
db에 잘 들어갔나 확인
성공!
'웹 > 백엔드, Spring' 카테고리의 다른 글
[Java] Spring과 SOLID - 2편 (0) | 2021.07.19 |
---|---|
[Java] Spring과 SOLID - 1편 (1) | 2021.07.19 |
[간단한 게시판 만들기] - 2. 로그인 기능( 세션, 쿠키 설정) (0) | 2020.10.17 |
[간단한 게시판 만들기] - 1. DB연동 (0) | 2020.10.17 |
[활용] Servlet, 싱글 파라미터 넘기기 (0) | 2020.09.18 |