💡 핵심 요약 (TL;DR)
Servlet게시판1: 기초의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* 사용처: 주소와 화면의 전환이 동시에 일어날때 (가상주소 .bo -> 가상주소로 .bo)을 적용할 수 있다.
web.xml코드 추가
1 2 3 4 5 6 7 8 9
| <servlet> <servlet-name>BoardFrontController</servlet-name> <servlet-class>com.itwillbs.board.action.BoardFrontController</servlet-class> </servlet> <servlet-mapping> <servlet-name>BoardFrontController</servlet-name> <url-pattern>*.bo</url-pattern> </servlet-mapping>
|
src폴더아래에 새로운 패키지와 BoardFrontController.java생성
extends HttpServlet상속을 해야 컨트롤러의 역할이 가능하다. 상속하지않으면 클래스명만 controller일 뿐 그냥 일반 클래스임.
- doGet(), doPost() 오버라이딩
- 주소 매핑
- 주소비교 후 처리
- 페이이지이동 : True면 sendRedirect이동방식, False면 forward이동 방식
- true면 sendRedirect방식으로 이동
- 사용처: 주소와 화면의 전환이 동시에 일어날때 (가상주소
.bo -> 가상주소로 .bo)
- false면 forward방식으로 이동
- 사용처: 주소는 그대로인데 화면이 바뀔때 (가상주소
.bo에서 request객체정보를 가지고 이동하면서 .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 56 57 58 59 60 61 62 63 64 65 66 67
| public class BoardFrontController extends HttpServlet{
protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doProcess() 호출 (페이지 GET/POST방식 모두 사용호출)"); System.out.println("--------------@ 주소 매핑 @-------------"); String requestURI = request.getRequestURI(); System.out.println(" requestURI : " + requestURI); String contextPath = request.getContextPath(); System.out.println(" contextPath : " + contextPath); String command = requestURI.substring(contextPath.length()); System.out.println(" command(가상주소) : " + command); System.out.println("--------------@ 주소 비교 후 처리 @-------------"); Action action = null; ActionForward forward = null; } System.out.println("-----------------@ 페이지 이동 @--------------"); if(forward != null){ if(forward.isRedirect()){ System.out.println("컨트롤러: "+forward.getPath()+"로 이동(sendRedirect이동방식)"); response.sendRedirect(forward.getPath()); }else{ System.out.println("컨트롤러: "+forward.getPath()+"로 이동(forward이동방식)"); RequestDispatcher dis = request.getRequestDispatcher(forward.getPath()); dis.forward(request, response); } } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet호출"); doProcess(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost호출"); doProcess(req, resp); }
}
|
ActionForward.java 생성
- sendRedirect 이동방식 : 주소와 화면의 전환이 동시에 일어날때 (가상주소 -> 가상주소로 )
- forward 이동방식 : 주소는 그대로인데 화면이 바뀔때 (가상주소에서 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
| public class ActionForward {
private String path; private boolean isRedirect; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public boolean isRedirect() { return isRedirect; } public void setRedirect(boolean isRedirect) { this.isRedirect = isRedirect; } @Override public String toString() { return "ActionForward [path=" + path + ", isRedirect=" + isRedirect + "]"; } }
|
Action.java생성
1 2 3 4 5 6 7
| public interface Action { public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) throws Exception; }
|
BoardDTO.java 생성
BoardBean.java 생성 대신 BoardBean.java 생성
- DTO(Data Transfer Object) 데이터 전송 객체
- DTO = bean = VO 동일한 역할
- JavaBeans 규칙을 따름
- DB에 정보를 가진 객체
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
| public class BoardDTO { private int bno; private String name; private String pw; private String subject; private String content; private int readcount; private int re_ref; private int re_lev; private int re_seq; private Date date; private String file; private String ip; public int getBno() { return bno; } (중략) @Override public String toString() { return "BoardBean [bno=" + bno + ", name=" + name + ", pw=" + pw + ", subject=" + subject + ", content=" + content + ", readcount=" + readcount + ", re_ref=" + re_ref + ", re_lev=" + re_lev + ", re_seq=" + re_seq + ", date=" + date + ", file=" + file + ", ip=" + ip + "]"; } }
|
BoardDAO.java 생성
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
| public class BoardDAO { Connection con = null; String sql = ""; PreparedStatement pstmt = null; ResultSet rs = null; private void getCon() throws Exception{ Context init = new InitialContext(); DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/mysqlDB"); con = ds.getConnection(); System.out.println("DAO: 디비연결성공"+con); } public void closeDB(){ try{ if(rs != null) rs.close(); if(pstmt != null) pstmt.close(); if(con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
|
#TILITWILL(JAVA)