💡 핵심 요약 (TL;DR)
Servlet일반상품1 : 기초의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* 쇼핑몰기능에서 일반 사용자가 보는 상품목록을 만들어보자.
`web.xml’에 코드 추가
쇼핑몰기능에서 일반 사용자가 보는 상품목록을 만들어보자.
web.xml 코드 수정 후 꼭 서버 실행 후 주소창에 test.bo해서 흰 화면이 뜨는지 확인하고 코딩할 것
흰화면이 안뜨면 web.xml 설정이 잘못되었다는 의미임.
1 2 3 4 5 6 7 8 9 <servlet > <servlet-name > GoodsFrontController</servlet-name > <servlet-class > com.itwillbs.goods.action.GoodsFrontController</servlet-class > </servlet > <servlet-mapping > <servlet-name > GoodsFrontController</servlet-name > <url-pattern > *.go</url-pattern > </servlet-mapping >
src폴더아래에 새로운 패키지와 GoodsFrontController.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 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 public class GoodsFrontController 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 + "]" ; } }
GoodsDTO.java 생성
관리자용 상품목록 출력할때 만들었던 GoodsDTO.java을 사용하면 된다.
추가생성 할 필요없다.
관리자용 상품DTO와 일반사용자용 상품DTO는 동일하기때문에 나눌 필요가 없다.
GoodsDAO.java 생성
DAO는 DTO와 달리 중요한 데이터에 접근하는 것이므로 관리자용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 28 public class GoodsDAO { 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)