💡 핵심 요약 (TL;DR)
Servlet쇼핑몰4: 메인페이지와 로그아웃의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* MemberLogout.java와 연결 => 12\. 로그아웃연결 :view페이지 필요없고 Action페이지가 필요함을 적용할 수 있다.
main.jsp 생성1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <%//1.한글처리, 파라미터 request.setCharacterEncoding("UTF-8"); String id = (String) session.getAttribute("id");//object를 string으로 다운캐스팅 String name = request.getParameter("name"); //1-1. id없이는 진입불가, id없는 경우 로그인페이지로 이동. 가상주소로 이동 if(id == null){ response.sendRedirect("./MemberLogin.me"); } %> <h3 > 아이디: <%=id %></h3 > <h3 > 아이디(session-EL표현식) : ${ sessionScope.id } </h3 > <input type ="button" value ="ITWILL게시판가기" class ="btn" onclick ="location.href='../board/boardList.jsp'" > <input type ="button" value ="회원정보조회" class ="btn" onclick ="location.href=''" > <input type ="button" value ="회원정보수정" class ="btn" onclick ="location.href=''" > <input type ="button" value ="로그아웃" class ="btn" onclick ="location.href='./MemberLogout.me'" > <input type ="button" value ="회원탈퇴" class ="btn" onclick ="location.href=''" >
MemberFrontController.java의 doProcess()메서드 추가 코드작성
main.jsp와 연결 => 11.메인페이지 연결
MemberLogout.java와 연결 => 12. 로그아웃연결 :view페이지 필요없고 Action페이지가 필요함
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 protected void doProcess (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doProcess메서드 호출" ); String requestURI = req.getRequestURI(); StringBuffer requestURL = req.getRequestURL(); String contextPath = req.getContextPath(); System.out.println("contextPath: " +contextPath); String command = requestURI.substring(contextPath.length()); System.out.println("잘 짤렸는지 가상주소 command: " +command); Action action = null ; ActionForward forward = null ; if (command.equals("/MemberJoin.me" )){ System.out.println("/MemberJoin.me 호출" ); forward = new ActionForward (); forward.setPath("./member/insertForm.jsp" ); forward.setRedirect(false ); }else if (command.equals("/MemberJoinAction.me" )){ System.out.println("/MemberJoinAction.me 주소 호출" ); action = new MemberJoinAction (); try { System.out.println("@@@@ Controller : MemberJoinAction 객체생성완료 후 excute()호출완료" ); forward = action.execute(req, resp); System.out.println("@@@@ Controller : 회원가입 처리완료 후 페이지 이동" +forward); } catch (Exception e) { e.printStackTrace(); } }else if (command.equals("/MemberLogin.me" )){ System.out.println("가상주소: /MemberLogin.me 주소 호출, 실제주소: ./member/loginForm.jsp" ); forward = new ActionForward (); forward.setPath("./member/loginForm.jsp" ); forward.setRedirect(false ); }else if (command.equals("/MemberLoginAction.me" )){ action = new MemberLoginAction (); try { System.out.println("@@@@ Controller : MembeLoginAction 객체생성완료 후 excute()호출완료" ); forward = action.execute(req, resp); System.out.println("@@@@ Controller : 로그인 처리완료 후 페이지 이동" +forward); } catch (Exception e) { e.printStackTrace(); } }else if (command.equals("/Main.me" )){ System.out.println("@@@@ Controller : 가상주소: /Main.me 실제주소:./member/main.jsp" ); forward = new ActionForward (); forward.setPath("./member/main.jsp" ); forward.setRedirect(false ); try { forward = action.execute(req, resp); } catch (Exception e) { e.printStackTrace(); } }else if (command.equals("/MemberLogout.me" )){ System.out.println("@@@@ Controller : 가상주소 /MemberLogout.me 실제주소: ./member/MemberLogoutAction.java" ); action = new MemberLogoutAction (); try { forward = action.execute(req, resp); } catch (Exception e) { e.printStackTrace(); } } if (forward != null ){ System.out.println("@@@페이지이동@@@" ); if (forward.isRedirect()){ System.out.println("sendRedirect방식의 이동" +forward.getPath()); resp.sendRedirect(forward.getPath()); }else { System.out.println("RequestDispatcher방식의 이동" +forward.getPath()); RequestDispatcher dis = req.getRequestDispatcher(forward.getPath()); dis.forward(req, resp); } } }
MemberLogoutAction.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 MemberLogoutAction implements Action { @Override public ActionForward execute (HttpServletRequest req, HttpServletResponse resp) throws Exception { System.out.println("@@@@ Action: MemberLogoutAction안의 execute() 실행됨" ); req.setCharacterEncoding("UTF-8" ); HttpSession session = req.getSession(); session.invalidate(); System.out.println("@@@@ Action : 로그아웃성공 후 페이지이동 " ); resp.setContentType("text/html; charset=UTF-8" ); PrintWriter out = resp.getWriter(); out.print("<script>" ); out.print("alert('정상적으로 로그아웃 되었습니다');" ); out.print("location.href='./Main.me';" ); out.print("</script>" ); out.close(); return null ; } }
#TIL ITWILL(JAVA)