๐Ÿ“„ Servlet & JSP ไธญ

๐Ÿ“– Servlet & JSP ไธญ

๐Ÿ“ฎ Servlet & JSP ์˜ˆ์ œ

์บก์ฒ˜

๐Ÿ“ Example

๐Ÿ”‘ ๋กœ๊ทธ์ธ

  1. ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ ๋ฉ”๋‰ด ํด๋ฆญ

  2. ๋ธŒ๋ผ์šฐ์ €์—์„œ /context/user?a=loginform request.

  3. user์— Mapping ๋˜์–ด์žˆ๋Š” UserServlet์˜ doGet() ํ˜ธ์ถœ

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            String actionName = request.getParameter("a");
            Action action = new UserActionFactory().getAction(actionName);
            action.execute(request, response);
        }
    
  4. a์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์“ฐ์—ฌ์ง„ loginform์„ actionName์ด๋ผ๋Š” String๊ฐ์ฒด๋กœ ๋ฐ›์•„

  5. UserActionFactory์—์„œ ํ•ด๋‹นํ•˜๋Š” Action ์‹คํ–‰

    else if("login".equals(actionName)) {
          action = new LoginAction();
    }
    
  6. Email๊ณผ Password๋ฅผ <form action="path">์œผ๋กœ ์ „๋‹ฌ ๋ฐ›์•„ UserDao์˜ get๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ผ์น˜ํ•˜๋Š” authUser ๊ฐ์ฒด ์ƒ์„ฑ

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
      String email = request.getParameter("email");
      String password = request.getParameter("password");
    
      UserVo authUser = new UserDao().get(email, password);
    
      if(authUser == null) {
        request.setAttribute("result", "fail");
        WebUtil.forward(request, response, "/WEB-INF/views/user/loginform.jsp");
        return;
      }
    
    //		๋กœ๊ทธ์ธ์ฒ˜๋ฆฌ
      HttpSession session = request.getSession(true);
      session.setAttribute("authUser", authUser);
    
      WebUtil.redirect(request, response, request.getContextPath()+"/main");
    
    }
    
  7. ์•ž์—์„œ ์ƒ์„ฑํ•œ authUser๋ฅผ session์— ์„ธํŒ…ํ•˜๊ณ 

  8. ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ Redirect.

๐Ÿ”ง ํšŒ์› ์ •๋ณด์ˆ˜์ •

  1. ๋กœ๊ทธ์ธ ํ›„ ์„ธ์…˜์ด ์‚ด์•„์žˆ๋Š” ์ƒํƒœ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ •๋ณด์ˆ˜์ • ๋งํฌ ํด๋ฆญ

  2. ๋ธŒ๋ผ์šฐ์ €์—์„œ /user?a=updateform request.

  3. user์— Mapping ๋˜์–ด์žˆ๋Š” UserServlet์˜ doGet() ํ˜ธ์ถœ

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String actionName = request.getParameter("a");
        Action action = new UserActionFactory().getAction(actionName);
        action.execute(request, response);
      }
    
  4. a์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์“ฐ์—ฌ์ง„ loginform์„ actionName์ด๋ผ๋Š” String๊ฐ์ฒด๋กœ ๋ฐ›์•„

  5. UserActionFactory์—์„œ ํ•ด๋‹นํ•˜๋Š” Action ์‹คํ–‰

    }else if("updateform".equals(actionName)) {
                action = new UpdateformAction();
    }
    
  6. ๋กœ๊ทธ์ธ ๋˜์–ด ์ €์žฅ์ƒํƒœ์˜ ์„ธ์…˜์„ ๊ฐ€์ ธ์˜ค๊ณ  ๋กœ๊ทธ์ธ๋˜์–ด์žˆ๋Š” ์•„์ด๋””์˜ ํ‚ค๊ฐ’ ์ถ”์ถœ, ํ‚ค๊ฐ’์œผ๋กœ ์ฐพ์€ User๋ฅผ authUser์„ธ์…˜์œผ๋กœ ์ƒˆ๋กœ ์ €์žฅ

    @Override
        public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // ์ ‘๊ทผ์ œ์–ด
            HttpSession session = request.getSession();
            if(session == null || session.getAttribute("authUser") == null) {
                WebUtil.redirect(request, response, request.getContextPath()+"/main");
                return;
            }
    
            UserVo authUser = (UserVo)session.getAttribute("authUser");
    
            if(authUser == null) {
                WebUtil.redirect(request, response, request.getContextPath());
                return;
            }
    
            Long userNo = authUser.getNo();
            UserVo userVo = new UserDao().get(userNo);
    
    
            request.setAttribute("userVo", userVo);
    
            session.setAttribute("authUser", authUser);
    
    
            WebUtil.forward(request, response, "/WEB-INF/views/user/updateform.jsp");
        }
    
  7. updateform.jsp ์—์„œ <form action="/?a=update">์œผ๋กœ ์ „๋‹ฌ ๋ฐ›์•„ UserServlet์—์„œ ๋‹ค์‹œ UpdateAction๊นŒ์ง€ ํผ๊ฐ’ ์ „๋‹ฌ

  8. UserDao.update(vo)๋ฅผ ํ†ตํ•ด update ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ ํ›„

    String sql = “update user set name=?, email=?, password=?, gender=? where no=?";

  9. ๋ณ€๊ฒฝ๋œ ๋‹‰๋„ค์ž„์ด ํ‘œ์‹œ๋˜๋„๋ก ๋ณ€๊ฒฝ๋œ User์ •๋ณด๋ฅผ ์„ธ์…˜์— ๋‹ค์‹œ ์‹ค์–ด ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ Redirect.

โ— URL ์š”์ฒญ -> Servlet -> ActionFactory -> ํŒ๋ณ„๋œ Action() ์‹คํ–‰ -> Redirect OR forward

๐ŸŒ Session / Request

๐Ÿ“จ Request

Method description
.getParamter(name) name์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
.getContextPath() ํ”„๋กœ์ ํŠธ Path

๐Ÿ“ก Session

์„œ๋ฒ„์ชฝ์— ์ƒํƒœ ์œ ์ง€๋ฅผ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š”๊ฒƒ

Method description
.getAttribute(String name) name์— ์ €์žฅ๋˜์–ด์žˆ๋Š” Object ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ ์—†์„ ๊ฒฝ์šฐ NULL
.setAttribute(String key , Object value) name์ธ ์†์„ฑ์— value ์˜ค๋ธŒ์ ํŠธ ํ• ๋‹น
.invalidate() ํ˜„์žฌ ์ƒ์„ฑ๋œ ์„ธ์…˜์„ ์ข…๋ฃŒ

โ†ช๏ธ Dispatcher / Redirect

๐Ÿ”— Dispatcher : forward(request, response)

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•˜๋ฉด์„œ ์ „์†กํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•œ๋‹ค. (๊ฐ™์€ request๊ฐ์ฒด๋ฅผ ๋ณด์กดํ•œ ์ƒํƒœ๋กœ ์ด๋™)

ใ€ฝ๏ธ Redirect : sendRedirect(url)

request๊ฐ์ฒด๊ฐ€ ์—†์–ด์ง€๋ฉด์„œ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ๋‹ค.

โœ”๏ธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ์ƒ๊ฐํ•˜์ž