💡 핵심 요약 (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 > AdminOrderFrontController</servlet-name > <servlet-class > com.itwillbs.admin.order.action.AdminOrderFrontController</servlet-class > </servlet > <servlet-mapping > <servlet-name > AdminOrderFrontController</servlet-name > <url-pattern > *.ao</url-pattern > </servlet-mapping >
AdminOrderFrontController.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 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 + "]" ; } }
AdminOrderDAO.java 인터페이스 생성1 2 3 4 5 6 public interface AdminOrderDAO { public List getAdminOrderList () ; }
AdminOrderDAOImpl.java 클래스 생성
위에서 만든 인터페이스를 상속받아서 DAO클래스를 만든다.
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 public class AdminOrderDAOImpl implements AdminOrderDAO { 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)