💡 핵심 요약 (TL;DR)
Servlet관리자용상품1 : 기초의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* 실무에서 필요한 모든 기능은 요구명세서에 작성된다. 그거 보고 처리하면 된다.
쇼핑몰기능을 만들려면 무슨 기능이 필요할까?
실무에서 필요한 모든 기능은 요구명세서에 작성된다. 그거 보고 처리하면 된다.
대략적으로 뭐가 필요할까? 상품, 구매자, 판매자, 주문(결제, 장바구니 등등), 재고관리 등등
관리자기눙 추가 : 상품등록
index.jsp 코드 변경1 2 3 4 <% //관리자-상품등록 response.sendRedirect("./GoodsAdd.ag"); %>
`web.xml’에 코드 추가 1 2 3 4 5 6 7 8 9 <servlet > <servlet-name > AdminGoodsFrontController</servlet-name > <servlet-class > com.itwillbs.admin.goods.action.AdminGoodsFrontController</servlet-class > </servlet > <servlet-mapping > <servlet-name > AdminGoodsFrontController</servlet-name > <url-pattern > *.ag</url-pattern > </servlet-mapping >
src폴더아래에 새로운 패키지와 AdminGoodsFrontController.java생성
일반 클래스는 controller역할을 할 수 없다. 일반 클래스를 서블릿을 상속해서 컨트롤러 역활 할수있도록 설정해야한다
따라서 HttpServlet 상속한 뒤 doGet(), doPost() 오버라이딩해야한다.
그리고 get방식이든 post방식이든 한 번에 처리할 수 있는 doProcess()를 생성한다.
doProcess()에서 처리하는 기능
주소 계산
command사용해서 주소 비교 후 처리
ActionForward를 가지고 페이지 이동
변수명 짓기 어려울때 변수명짓기 사이트
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 public class AdminGoodsFrontController 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 + "]" ; } }
#TIL ITWILL(JAVA)