๐ŸŒฑ Spring MVC (1) Servlet, JSP : ์˜ˆ์ „ ๋ฐฉ์‹์œผ๋กœ ๊ฐ„๋‹จํ•œ ํšŒ์› ๊ฐ€์ž…, ์กฐํšŒ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž‘์„ฑ

Kim Dae Hyunยท2021๋…„ 7์›” 1์ผ
0

Spring-MVC

๋ชฉ๋ก ๋ณด๊ธฐ
1/13
post-thumbnail

Github ์†Œ์Šค์ฝ”๋“œ

Spring MVC๊ฐ€ ์–ผ๋งˆ๋‚˜ ํŽธํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ฃผ๊ณ  ๋˜ ๊ทธ ๊ธฐ๋Šฅ์˜ ๋‚ด๋ถ€๋Š” ์–ด๋–ค ์‹์œผ๋กœ ์ด๋ฃจ์–ด์กŒ๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์•„์ฃผ ๋ณ„๋กœ์ธ ์˜ˆ์ „ ๋ฐฉ์‹์˜ Servlet๊ณผ JSP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด๋ณด์ž.

๐Ÿ”Ž ์ด๋Ÿฐ๊ฑฐ ํ•ด๋ณผ ๊ฑฐ์—์š”

1. servlet๋งŒ์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„

2. JSP๋งŒ์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„

3. Servlet + JSP MVCํŒจํ„ด ์ ์šฉ


๐Ÿ“Œ ํ”„๋กœ์ ํŠธ ์ค€๋น„ (๊ณตํ†ต)

  • spring์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด์ง€๋งŒ ๋‚ด์žฅ tomcat๊ณผ ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ์„ ์œ„ํ•ด ๋‘ ๊ฐœ dependency๋ฅผ ๊ฐ–๋Š” springboot application์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • https://start.spring.io/
  • ์œ„ ์‚ฌ์ดํŠธ์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Spring web๊ณผ Lombok ์„ dependency๋กœ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

๐Ÿ“Œ ํ”„๋กœ์ ํŠธ ํ…Œ์ŠคํŠธ (๊ณตํ†ต)

  • Servlet์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด mainํด๋ž˜์Šค์— @ServletComponentScan ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
@SpringBootApplication
@ServletComponentScan

public class ServletApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServletApplication.class, args);
	}

}
  • mainํด๋ž˜์Šค๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์•„๋ž˜ ํ™”๋ฉด์ด ๋‚˜์˜ค๋ฉด ํ”„๋กœ์ ํŠธ ์ค€๋น„๊ฐ€ ์ž˜ ๋œ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

๐Ÿ“Œ ๋„๋ฉ”์ธ ํด๋ž˜์Šค ์ž‘์„ฑ (๊ณตํ†ต)

  • ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ ํ˜น์€ DB์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์ค„ ๋„๋ฉ”์ธ ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ์šฐ๋ฆฌ๋Š” username๊ณผ age๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๊ฒƒ์ด๊ธฐ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.
@Getter @Setter
public class Member {

    private Long id;
    private String username;
    private int age;

    public Member() {
    }

    public Member(String username, int age) {
        this.username = username;
        this.age = age;
    }
}

๐Ÿ“Œ Repository ์ž‘์„ฑ (๊ณตํ†ต)

  • ์ €์žฅ์†Œ๋ฅผ ์œ„ํ•œ Repository ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • DB๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (๊ทธ๋ƒฅ ์„œ๋ฒ„๊ฐ€ ์ผœ์ ธ์žˆ๋Š” ๋™์•ˆ ์ „์—ญ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.)
  • ์ €์žฅ(save), ID๋กœ ์ฐพ๊ธฐ(findById), ์ „์ฒด์กฐํšŒ(FindAll) 3๊ฐœ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
public class MemberRepository {

    private static Map<Long, Member> store = new HashMap<>();
    private static Long sequence = 0L;

    private static final MemberRepository instance = new MemberRepository();

    private MemberRepository() {
    }

    public static MemberRepository getInstance() {
        return instance;
    }

    public Member save(Member member){
        member.setId(++sequence);
        store.put(member.getId(), member);
        return member;
    }

    public Member findById(Long id) {
        return store.get(id);
    }

    public List<Member> findAll() {
        return new ArrayList<>(store.values());
    }

    public void clearAll() {
        store.clear();
    }
}

๐Ÿ‘‰๐Ÿป ์ฐธ๊ณ 

static ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ private๋กœ ํ•˜์—ฌ ์™ธ๋ถ€์— ํ˜ธ์ถœ์„ ๋ง‰๋Š” ์ด์œ 

  • Repository ํด๋ž˜์Šค๋ฅผ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ ์ž…๋‹ˆ๋‹ค.
  • ์‹ฑ๊ธ€ํ†ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‹จ ํ•œ ๊ฐœ๋งŒ ์ƒ์„ฑ๋˜์–ด ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ๋‹จ ํ•œ ๊ฐœ์˜ ๊ฐ์ฒด๋งŒ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.
  • ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ static์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ์ปดํŒŒ์ผ ํƒ€์ž„์— ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•˜๋Š” ์ชฝ์—์„œ๋Š” getInstance ๋ฉ”์„œ๋“œ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊นŒ์ง€ ๋ง‰์•„๋‘์—ˆ์œผ๋ฏ€๋กœ ์™ธ๋ถ€์—์„œ new ๋กœ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

1-1 Form์„ ๋žœ๋”๋งํ•˜๊ธฐ ์œ„ํ•œ Servlet ์ž‘์„ฑ

  • Servlet์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด HttpServlet์„ ์ƒ์†๋ฐ›๊ณ  service ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ฉ๋‹ˆ๋‹ค.
  • @WebServlet url๋งคํ•‘ ์ •๋ณด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • PrintWriter ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ html์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋ณด์ด์‹ญ๋‹ˆ๊นŒ?
    • IDE์˜ ๋„์›€์—†์ด.... ์ด๊ฑด ์ข€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์ผ๋‹จ ํ•ด๋ด…์‹œ๋‹ค..
@WebServlet(name = "memberFormServlet", urlPatterns = "/servlet/members/new-form")
public class MemberFormServlet extends HttpServlet {    

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        res.setContentType("text/html");
        res.setCharacterEncoding("utf-8");

        PrintWriter w = res.getWriter();
        w.write("<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<form action=\"/servlet/members/save\" method=\"post\">\n" +
                "    username: <input type=\"text\" name=\"username\" />\n" +
                "    age:      <input type=\"text\" name=\"age\" />\n" +
                " <button type=\"submit\">์ „์†ก</button>\n" + "</form>\n" +
                "</body>\n" +
                "</html>\n");
    }
}
  • ์•„๋ž˜์™€ ๊ฐ™์€ Form์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

1-2 ์ €์žฅ์„ ์œ„ํ•œ Servlet ์ž‘์„ฑ

  • MemberRepository ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค.
  • ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ HttpServlet์„ ์ƒ์†๋ฐ›๊ณ  service๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  @WebServlet์— ๋งคํ•‘์ •๋ณด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
    • Form์„ ๋žœ๋”๋งํ•˜๋Š” servlet์˜ form action url๊ณผ ์ž˜ ๋งž์ถฐ์ฃผ์„ธ์š”.
  • Request๊ฐ์ฒด์˜ getParameter๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ „์†ก๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑํ•ฉ๋‹ˆ๋‹ค.
  • ํŒŒ์‹ฑ๋œ ๋ฐ์ดํ„ฐ๋กœ Member๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Repository์˜ save๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ €์žฅ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•œ html๋„ ๊ตฌ์„ฑํ•ด์ค๋‹ˆ๋‹ค. (....)
@WebServlet(name = "memberSaveServlet", urlPatterns = "/servlet/members/save")
public class MemberSaveServlet extends HttpServlet {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        String username = req.getParameter("username");
        int age = Integer.parseInt(req.getParameter("age"));

        Member member = new Member(username, age);

        memberRepository.save(member);

        res.setContentType("text/html");
        res.setCharacterEncoding("utf-8");
        PrintWriter w = res.getWriter();
        w.write("<html>\n" +
                "<head>\n" +
                " <meta charset=\"UTF-8\">\n" + "</head>\n" +
                "<body>\n" +
                "์„ฑ๊ณต\n" +
                "<ul>\n" +
                "    <li>id="+member.getId()+"</li>\n" +
                "    <li>username="+member.getUsername()+"</li>\n" +
                " <li>age="+member.getAge()+"</li>\n" + "</ul>\n" +
                "<a href=\"/index.html\">๋ฉ”์ธ</a>\n" + "</body>\n" +
                "</html>");
    }
}
  • ๋ฐ์ดํ„ฐ ์ „์†ก์‹œ ์•„๋ž˜์™€ ๊ฐ™์€ ํŽ˜์ด์ง€๊ฐ€ ๋žœ๋”๋ง ๋ฉ๋‹ˆ๋‹ค.

1-3 ์ „์ฒด ๋ฐ์ดํ„ฐ ์กฐํšŒ

  • MemberRepository์˜ findAll ์ €์žฅ์†Œ์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค.
  • ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ foreach๋ฌธ์„ ์ด์šฉํ•˜์—ฌ html์„ ๊ตฌ์„ฑํ•ด์ค๋‹ˆ๋‹ค.
@WebServlet(name = "memberListServlet", urlPatterns = "/servlet/members")
public class MemberListServlet extends HttpServlet {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        List<Member> members = memberRepository.findAll();

        res.setContentType("text/html");
        res.setCharacterEncoding("utf-8");

        PrintWriter w = res.getWriter();
        w.write("<html>");
        w.write("<body>");
        for (Member member : members) {
            w.write("<ul>");
            w.write("<li>");
            w.write("<span> username: "+member.getUsername()+" age: "+ member.getAge()+"</span>");
            w.write("</li>");
            w.write("</ul>");
        }
        w.write("</body>");
        w.write("</html>");
    }
}
  • ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„ค์š”.
  • Servlet์€ JAVA์ฝ”๋“œ๋กœ HTML์„ ์ž‘์„ฑํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ๋ถˆํŽธํ•ฉ๋‹ˆ๋‹ค.
  • ๋˜ํ•œ ์ด๋ ‡๊ฒŒ JAVA์ฝ”๋“œ์™€ HTML์ฝ”๋“œ๊ฐ€ ํ˜ผ์žฌ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์€ ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ, ์œ ์ง€๋ณด์ˆ˜์„ฑ ๋“ฑ ๋ชจ๋“  ๋ฉด์—์„œ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

2-1 Form์„ ๋žœ๋”๋งํ•˜๊ธฐ ์œ„ํ•œ JSP ํŒŒ์ผ ์ž‘์„ฑ

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/jsp/members/save.jsp" method="post">
        username: <input type="text" name="username"/>
        age: <input type="text" name="age"/>
        <button type="submit">์ „์†ก</button>
    </form>
</body>
</html>

2-2 ์ €์žฅ์„ ์œ„ํ•œ JSP ํŒŒ์ผ ์ž‘์„ฑ

  • <% %> ๋‚ด์— Java์ฝ”๋“œ ์ž‘์„ฑ
  • <%= %> ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅ
  • .jsp ํŒŒ์ผ ๋‚ด์—์„œ request, resoponse ๊ฐ์ฒด๋Š” ๋ณ„๋„ ์„ ์–ธ ์—†์ด ์˜ˆ์•ฝ์–ด์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
<%@ page import="basic.servlet.domain.member.MemberRepository" %>
<%@ page import="basic.servlet.domain.member.Member" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  MemberRepository memberRepository = MemberRepository.getInstance();
  String username = request.getParameter("username");
  int age = Integer.parseInt(request.getParameter("age"));

  Member member = new Member(username, age);

  memberRepository.save(member);
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<ul>
  <li>id=<%=member.getId()%></li>
  <li>username=<%=member.getUsername()%></li>
  <li>age=<%=member.getAge()%></li>
</ul>
<a href="/index.html">๋ฉ”์ธ</a>
</body>
</html>

2-3 ์ „์ฒด ๋ฐ์ดํ„ฐ ์กฐํšŒ

  • out ๋˜ํ•œ request, response์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ ์–ธ ์—†์ด ์˜ˆ์•ฝ์–ด์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
<%@ page import="basic.servlet.domain.member.MemberRepository" %>
<%@ page import="basic.servlet.domain.member.Member" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  MemberRepository memberRepository = MemberRepository.getInstance();
  List<Member> members = memberRepository.findAll();
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="/index.html">๋ฉ”์ธ</a>
<table>
  <thead>
  <th>id</th>
  <th>username</th>
  <th>age</th>
  </thead>
  <tobdy>
    <%
      for (Member member : members) {
        out.write("<tr>");
        out.write("   <td>"+member.getId()+"</td>");
        out.write("   <td>"+member.getUsername()+"</td>");
        out.write("   <td>"+member.getAge()+"</td>");
        out.write("</tr>");
      }
    %>
  </tobdy>
</table>
</body>
</html>

JSP๊ฐ€ ์ˆœ์ˆ˜ servlet๋ณด๋‹ค๋Š” HTML์„ ๊ตฌ์„ฑํ•˜๋Š”๋ฐ๋Š” ํ›จ์”ฌ ํŽธํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”.


3-1 Form์„ ์œ„ํ•œ JSPํŒŒ์ผ ์ž‘์„ฑ

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<!-- ์ƒ๋Œ€๊ฒฝ๋กœ ์‚ฌ์šฉ => localhost:8080/test/page.jsp ์—์„œ ์ด jspํŒŒ์ผ ํ˜ธ์ถœ์‹œ
     action url์€ localhost:8080/test/save๊ฐ€ ๋จ-->
<form action="save" method="post">
    username: <input type="text" name="username"/>
    age: <input type="text" name="age"/>
    <button type="submit">์ „์†ก</button>
</form>
</body>
</html>

3-2 Form ๋žœ๋”๋ง ์š”์ฒญ์„ ์œ„ํ•œ Servlet ์ž‘์„ฑ

urlPattenrs์— ์ง€์ •ํ•œ ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์ด ์˜ค๋ฉด Form์ด ์žˆ๋Š” .jspํŒŒ์ผ์„ ์‘๋‹ตํ•˜๊ธฐ ์œ„ํ•œ Servlet์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์ด ์™”์„ ๋•Œ jspํŒŒ์ผ์„ ์‘๋‹ตํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. (jspํŒŒ์ผ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•ด์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค.)

  • Redirect
    • ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์ด ์™”์„ ๋•Œ test.jsp๋ฅผ ์‘๋‹ตํ•˜๋ ค๋ฉด ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ test.jsp๋กœ ์ด๋™ํ•˜๋ผ๋Š” ์‘๋‹ต์„ ๋ณด๋‚ด๊ณ  ๋‹ค์‹œ ํด๋ผ์ด์–ธํŠธ์˜ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์„œ๋ฒ„์— test.jsp๋ฅผ ์š”์ฒญํ•ด์•ผ ํ•œ๋‹ค.
    • ์ฆ‰, 2๋ฒˆ์˜ ์š”์ฒญ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • Dispatcher.forward
    • ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์ด ์™”์„ ๋•Œ test.jsp๋ฅผ ์‘๋‹ตํ•˜๋ ค๋ฉด ์„œ๋ฒ„ ๋‚ด๋ถ€์ ์œผ๋กœ test.jsp์— ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•ด์ค€๋‹ค.
    • ์ฆ‰ ํด๋ผ์ด์–ธํŠธ์˜ ๋ธŒ๋ผ์šฐ์ €๋Š” 1๋ฒˆ์˜ ์š”์ฒญ์œผ๋กœ test.jsp๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค ์š”์ฒญ์€ ์ถ”๊ฐ€๋กœ ์„œ๋ฒ„ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐœ์ƒํ•˜์˜€์ง€๋งŒ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด ์‚ฌ์‹ค์„ ์•Œ์ง€ ๋ชปํ•œ๋‹ค.
@WebServlet(name = "mvcMemberFormServlet", urlPatterns = "/servlet-mvc/members/new-form")
public class MvcMemberFormServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        String viewPath = "/WEB-INF/views/new-form.jsp";
        RequestDispatcher dispatcher = req.getRequestDispatcher(viewPath);
        dispatcher.forward(req, res);
    }
}

3-3 ์ €์žฅ์„ ์œ„ํ•œ Servlet ์ž‘์„ฑ

MemberRepository๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ €์žฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋žœ๋”๋ง์„ ์œ„ํ•ด save-result.jsp์— ์ „๋‹ฌํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • request.setAttribute("key", value);
    • Model์„ ์ด์šฉํ•˜์—ฌ View๋‹จ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.
  • View ๋‹จ์—์„œ Model์— ๋‹ด์•„์ค€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฟŒ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•ด์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    (save-result.jsp)
@WebServlet(name = "mvcMemberSaveServlet", urlPatterns = "/servlet-mvc/members/save")
public class MvcMemberSaveServlet extends HttpServlet {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        String username = req.getParameter("username");
        int age = Integer.parseInt(req.getParameter("age"));

        Member member = new Member(username, age);

        memberRepository.save(member);

        String viewPath = "/WEB-INF/views/save-result.jsp";
        RequestDispatcher dispatcher = req.getRequestDispatcher(viewPath);
        req.setAttribute("member",member);
        dispatcher.forward(req, res);
    }
}

3-4 ์ €์žฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋žœ๋”๋งํ•ด์ฃผ๊ธฐ ์œ„ํ•œ JSP ํŒŒ์ผ ์ž‘์„ฑ

<%= %> ํ˜น์€ ${ } ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ Model๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฟŒ๋ ค์ค๋‹ˆ๋‹ค.

  • setAttibute()๋กœ Model์— ๊ฐ’์„ ์„ธํŒ…ํ•˜๊ณ  getAttribute()๋กœ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>
<!-- ๋ฐฉ๋ฒ• 1
     getAttribute()๋กœ ๋ฐ›์€ ๊ฐ’์ด object ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ์บ์ŠคํŒ…์ด ํ•„์š”ํ•จ .. ์กฐ๊ธˆ ๋ฒˆ๊ฑฐ๋กœ์›€-->
<%--<ul>--%>
<%--  <li>id=<%=((Member)request.getAttribute("member")).getId()%></li>--%>
<%--  <li>username=<%=((Member)request.getAttribute("member")).getUsername()%></li>--%>
<%--  <li>age=<%=((Member)request.getAttribute("member")).getAge()%></li>--%>
<%--</ul>--%>

<!-- ๋ฐฉ๋ฒ•2 -->
<ul>
  <li>id=${member.id}</li>
  <li>username=${member.username}</li>
  <li>age=${member.age}</li>
</ul>
<a href="/index.html">๋ฉ”์ธ</a>
</body>
</html>

3-5 ์ „์ฒด ๋ฐ์ดํ„ฐ ์กฐํšŒ๋ฅผ ์œ„ํ•œ Servlet ์ž‘์„ฑ

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ MemberRepository์˜ findAll() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๊ณ  ์ฝ์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ Model์— ๋‹ด์•„ View๋‹จ์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

  • Objectํƒ€์ž…์œผ๋กœ ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— List๋„ ๊ด€๊ณ„์—†์ด Model์— ๋‹ด์•„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@WebServlet(name = "mvcMemberListServlet", urlPatterns = "/servlet-mvc/members")
public class MvcMemberListServlet extends HttpServlet {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        List<Member> members = memberRepository.findAll();

        req.setAttribute("members",members);
        String viewPath = "/WEB-INF/views/members.jsp";
        RequestDispatcher dispatcher = req.getRequestDispatcher(viewPath);
        dispatcher.forward(req, res);
    }
}

3-6 ์ „์ฒด ๋ฐ์ดํ„ฐ ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ๋žœ๋”๋งํ•˜๊ธฐ ์œ„ํ•œ JSPํŒŒ์ผ ์ž‘์„ฑ

  • JSTL core tag๋ฅผ ์ด์šฉํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค Model์— ๋‹ด๊ธด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
  <title>Title</title>
</head>
<body>
<a href="/index.html">๋ฉ”์ธ</a>
<table>
  <thead>
  <th>id</th>
  <th>username</th>
  <th>age</th>
  </thead>
  <tobdy>
  <c:forEach var="member" items="${members}">
    <tr>
      <td>${member.id}</td>
      <td>${member.username}</td>
      <td>${member.age}</td>
    </tr>
  </c:forEach>
  </tobdy>
</table>
</body>
</html>

๐Ÿ‘‰๐Ÿป ์ฐธ๊ณ 

  • /WEB-INF ์ดํ•˜์— ์žˆ๋Š” ํŒŒ์ผ์€ ์™ธ๋ถ€์—์„œ ์ง์ ‘ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ฆ‰, ์™ธ๋ถ€์—์„œ ๋ฐ”๋กœ ์ ‘๊ทผ์‹œํ‚ค๊ณ  ์‹ถ์ง€ ์•Š์€ ํŒŒ์ผ์„ ์œ„์น˜์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ๋“ค์–ด members.jsp (์ „์ฒด ํšŒ์›์˜ ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ jspํŒŒ์ผ)์— ์ง์ ‘ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋กœ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  servlet์„ ํ†ตํ•ด ์ ‘๊ทผํ•˜๊ฒŒ ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง MVC 1ํŽธ ์„ ์ˆ˜๊ฐ•ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€