💡 핵심 요약 (TL;DR)
Servlet구매하기1 : 기초의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* 흰화면이 안뜨면 web.xml 설정이 잘못되었다는 의미임.
`web.xml’에 코드 추가
web.xml 코드 수정 후 꼭 서버 실행 후 주소창에 test.bo해서 흰 화면이 뜨는지 확인하고 코딩할 것
흰화면이 안뜨면 web.xml 설정이 잘못되었다는 의미임.
1 2 3 4 5 6 7 8 9 <servlet > <servlet-name > OrderFrontController</servlet-name > <servlet-class > com.itwillbs.order.action.OrderFrontController</servlet-class > </servlet > <servlet-mapping > <servlet-name > OrderFrontController</servlet-name > <url-pattern > *.or</url-pattern > </servlet-mapping >
OrderFrontController.java의 doProcess()의 주소비교 후 처리부분에 코드 추가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 public class OrderFrontController 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("C : " +forward.getPath()+"주소로 이동(Redirect)" ); response.sendRedirect(forward.getPath()); }else { System.out.println("C : " +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() 호출 (페이지 GET방식 호출)" ); doProcess(req, resp); } @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost() 호출 (페이지 POST방식 호출)" ); doProcess(req, resp); } }
Action.java 생성1 2 3 4 5 6 7 8 9 10 11 12 13 public interface Action { public ActionForward execute (HttpServletRequest request, HttpServletResponse response) throws Exception; }
ActionForward.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 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 + "]" ; } }
OrderDTO.java생성
DB테이블을 생성후 DTO를 아래 코드와 같이 생성한다
DB테이블 생성시 주의사항 : 테이블명 order 불가능
why? order by랑 비슷해서 테이블명이 아닌 명령어로 처리하려하기 때문.
그럼 실무에선 주로 어떻게 쓸까?
tbl_order : 실무에서 자주 사용함
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 public class OrderDTO { private int o_num; private String o_trade_num; private int o_g_num; private String o_g_name; private int o_g_amount; private String o_g_size; private String o_g_color; private String o_m_id; private String o_receive_name; private String o_receive_addr1; private String o_receive_addr2; private String o_receive_phone; private String o_receive_memo; private int o_sum_money; private String o_trade_type; private String o_trade_payer; private Date o_trade_date; private String o_trans_num; private Date o_date; private String o_status; public int getO_num () { return o_num; } public void setO_num (int o_num) { this .o_num = o_num; } public String getO_trade_num () { return o_trade_num; } (중략) }
OrderDAO.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 27 public class OrderDAO { Connection con = null ; PreparedStatement pstmt = null ; ResultSet rs = null ; String sql = "" ; private void getCon () throws Exception{ Context init = new InitialContext (); DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/model2DB" ); 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(); } } }
#TIL ITWILL(JAVA)