# Spring / ์ˆœ์ˆ˜ Servlet ํŒจํ„ด, JSP ํŒจํ„ด
Study Repository

Spring / ์ˆœ์ˆ˜ Servlet ํŒจํ„ด, JSP ํŒจํ„ด

by rlaehddnd0422

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ํšŒ์›๊ฐ€์ž…, ํšŒ์›์กฐํšŒ ๋กœ์ง์„ MVC ํŒจํ„ด์„ ์‚ดํŽด๋ณด๊ธฐ์— ์•ž์„œ ์ˆœ์ˆ˜ Servlet ํŒจํ„ด๊ณผ JSP ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด๋ณด๊ณ , ๊ฐ ํŒจํ„ด์˜ ํ•œ๊ณ„์™€ ๋‹จ์ ์„ ์‚ดํŽด๋ณด๊ณ  ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ MVC ํŒจํ„ด์„ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํšŒ์› ๋„๋ฉ”์ธ ์ •๋ณด

  • ์ด๋ฆ„ : username
  • ๋‚˜์ด : age

๊ธฐ๋Šฅ

  • ํšŒ์› ์ €์žฅ
  • ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

์ˆœ์ˆ˜ Servlet

์šฐ์„  ํšŒ์› ๋„๋ฉ”์ธ ์ •๋ณด๋ฅผ ๋งŒ๋“ค์–ด์•ผ๊ฒ ์ฃ .

Lombok ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ Getter, Setter๋Š” ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

Member.java

package hello.servlet.basic.domain.member;

import lombok.Getter;
import lombok.Setter;

@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;
    }
}

 

๊ทธ๋ฆฌ๊ณ  ๋„๋ฉ”์ธ์„ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•ด ๋„๋ฉ”์ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋„ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ๊ฒ ์ฃ . HashMap์œผ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

MemberRepository.java

package hello.servlet.basic.domain.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemberRepository {
    private Map<Long,Member> store = new HashMap<>();
    private static long sequence = 0L;
    private static final MemberRepository instance = new MemberRepository();
    public static MemberRepository getInstance()
    {
        return instance;
    }
    private MemberRepository(){}

    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 clearStore()
    {
        store.clear();
    }
}

์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ( ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ํ†ฐ์บฃ์„œ๋ฒ„๋งŒ ์ด์šฉ ) MemberRepository๋ฅผ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด,

1. private MemberRepository ์ƒ์„ฑ์ž๋กœ ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐด

2. ํด๋ž˜์Šค๋ ˆ๋ฒจ์— MemberRepository์„ final๋กœ ์„ ์–ธํ•ด ๋ณ€๊ฒฝ ๋ฐด

3. getInstance()๋กœ ํ•ญ์ƒ ๊ฐ™์€ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์กฐํšŒํ•˜๋„๋ก ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.  

 

์ด์ œ ์„œ๋ธ”๋ฆฟ๋งŒ์„ ์‚ฌ์šฉํ•ด์„œ ํšŒ์› ๋“ฑ๋ก, ์ €์žฅ, ์กฐํšŒ ๋กœ์ง์„ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋กœ, ๋“ฑ๋ก ๋กœ์ง์ž…๋‹ˆ๋‹ค.

@WebServlet(name="memberFormServlet",urlPatterns = "/servlet/members/new-form")
public class MemberFormServlet extends HttpServlet {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

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

        PrintWriter w = response.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");
    }
}

1. ๋“ฑ๋ก -servlet/members/new-form

1. HTML Form์„ ํ†ตํ•ด์„œ ํšŒ์›์„ ๋“ฑ๋กํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, Content-type์„ text/html;charset=utf-8 ์œผ๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. 

 

2. ์„œ๋ธ”๋ฆฟ์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ํ˜•ํƒœ๋Š” ๋ฌธ์ž๋‚˜ ๋ฐ”์ดํŠธํ˜•ํƒœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋ฌธ์ž ํ˜•ํƒœ๋กœ ์‘๋‹ตํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ์˜ Stream์„ ์ปจํŠธ๋กคํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, ํ…์ŠคํŠธํ˜•ํƒœ๋กœ ์‘๋‹ต์„ ๋ณด๋‚ด๋„๋ก ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด PrintWriter ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด getWriter() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์‘๋‹ต์œผ๋กœ ๋‚ด๋ณด๋‚ผ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์–ป์–ด๋‚ธ ํ›„ ์ŠคํŠธ๋ฆผ์— ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๋กํ–ˆ์–ด์š”.

 

3. ์ „์†ก ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด <form> tag์˜ action์„ servlet/members/save๋กœ, method๋ฅผ POST๋กœ ์ง€์ •ํ•ด ์ž…๋ ฅํ•œ ํผ์„ servlet/members/save๋กœ POSTํ•˜๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

<์ฐธ๊ณ ์ž๋ฃŒ>

https://charliecharlie.tistory.com/353

 

[Servlet&JSP] PrintWriter out = response.getWriter(); ์˜ ๋™์ž‘ ์›๋ฆฌ

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ Servlet์œผ๋กœ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์š”์ฒญ(Request)์„ ํŒŒ์•…ํ•œ ํ›„ ์‘๋‹ต(Response)์„ ์ „๋‹ฌํ•œ๋‹ค. Servlet์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ํ…์ŠคํŠธ(HTML) ํ˜•ํƒœ๋กœ ์‘๋‹ต์„ ๋ณด๋‚ด๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‘๋‹ต์œผ๋กœ ๋‚ด๋ณด๋‚ผ ์ถœ

charliecharlie.tistory.com

 

๋‘ ๋ฒˆ์งธ๋กœ, ์ €์žฅ ๋กœ์ง์ž…๋‹ˆ๋‹ค.

์•ž์„  ๋“ฑ๋ก ๋กœ์ง์—์„œ HTML Form์˜ ๋‚ด์šฉ์„ servlet/members/save์— POST ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. 

์ €์žฅ ๋กœ์ง์—์„œ๋Š”

1. ๋“ฑ๋ก ๋กœ์ง์—์„œ์˜ HTML Form์œผ๋กœ๋ถ€ํ„ฐ ์ „์†ก๋œ ๋„๋ฉ”์ธ ์ •๋ณด๋ฅผ ํ†ตํ•ด ๋„๋ฉ”์ธ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“  ๋‹ค์Œ,์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์ƒ์„ฑํ•œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— save ํ•ฉ๋‹ˆ๋‹ค.

2. servlet/members/save์— POST๋œ ๋„๋ฉ”์ธ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

@WebServlet(name="memberSaveServlet",urlPatterns = "/servlet/members/save")
public class MemberSaveServlet extends HttpServlet {

    private MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        int age = Integer.parseInt(request.getParameter("age"));
        Member member = new Member(username,age);
        memberRepository.save(member);

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter w = response.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>");
    }
}

2. ์ €์žฅ - servlet/members/save

์šฐ์„  response๋กœ ๋ถ€ํ„ฐ username๊ณผ age๋ฅผ getParameter()์„ ์‚ฌ์šฉํ•ด์„œ ์š”์ฒญ๋ฐ›๊ณ  ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ํ†ตํ•ด, ๋„๋ฉ”์ธ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„ ๋„๋ฉ”์ธ์„ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ๊ธฐ๋กํ–ˆ์–ด์š”.

 

๊ทธ ๋‹ค์Œ ๋„๋ฉ”์ธ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ getterํ•˜์—ฌ PrintWriter ์ถœ๋ ฅ์ŠคํŠธ๋ฆผ์„ ํ†ตํ•ด html ๋ฐฉ์‹์œผ๋กœ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

( setContentType, setCharacterEncoding ์„ ํ†ตํ•ด Content-type : text/html;charset=utf-8 ๋กœ ๋ฏธ๋ฆฌ ์ง€์ • )

 

์„ธ ๋ฒˆ์งธ๋กœ ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ์ž…๋‹ˆ๋‹ค.

package hello.servlet.web.servlet;

import hello.servlet.basic.domain.member.Member;
import hello.servlet.basic.domain.member.MemberRepository;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name="memberListServlet",urlPatterns = "/servlet/members")
public class MemberListServlet extends HttpServlet {
    MemberRepository memberRepository = MemberRepository.getInstance();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

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

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

        PrintWriter w = response.getWriter();

        w.write("<html>");
        w.write("<head>");
        w.write("    <meta charset=\"UTF-8\">");
        w.write("    <title>Title</title>");
        w.write("</head>");
        w.write("<body>");
        w.write("<a href=\"/index.html\">๋ฉ”์ธ</a>");
        w.write("<table>");
        w.write("    <thead>");
        w.write("    <th>id</th>");
        w.write("    <th>username</th>");
        w.write("    <th>age</th>");
        w.write("    </thead>");
        w.write("    <tbody>");

        for (Member member : members)
        {
            w.write("    <tr>");
            w.write("        <td>" + member.getId() + "</td>");
            w.write("        <td>" + member.getUsername() + "</td>");
            w.write("        <td>" + member.getAge() + "</td>");
            w.write("    </tr>");
        }
        w.write("    </tbody>");
        w.write("</table>");
        w.write("</body>");
        w.write("</html>");
    }
}
  1. URI๋Š” /servlet/members๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , List์— findAll ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ๊ธฐ๋ก๋œ ๋„๋ฉ”์ธ ์ •๋ณด๋“ค์„ ์ €์žฅ.
  2. HTML ํ˜•์‹์œผ๋กœ ํšŒ์›๋ชฉ๋ก์„ ๋ฟŒ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ปจํ…์ธ ํƒ€์ž…์€ html, ์ธ์ฝ”๋”ฉ์€ utf-8๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

3. ์กฐํšŒ - servlet/members

 

์ด๋ ‡๊ฒŒ ํ•ด์„œ ์•„์ฃผ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„œ๋ธ”๋ฆฟ๊ณผ ์ž๋ฐ”์ฝ”๋“œ๋งŒ์œผ๋กœ HTML์„ ๋งŒ๋“ค์–ด๋ณด์•˜๋Š”๋ฐ, ์„œ๋ธ”๋ฆฟ ๋•๋ถ„์— ๋™์ ์œผ๋กœ HTML์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ,

์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋งค์šฐ ๋ณต์žกํ•˜๊ณ  ๋น„ํšจ์œจ์ ์ž„์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”. ๋ฐ‘์— ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ผ์ผํžˆ ๋‹ค ์ €๋ ‡๊ฒŒ HTML์„ ์ž‘์„ฑํ•˜๋Š” ๊ฑด ๊ต‰์žฅํžˆ ๋ถˆํŽธํ•˜๊ณ  ๊ท€์ฐฎ๊ณ  ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

w.write("    <meta charset=\"UTF-8\">");
w.write("    <title>Title</title>");
w.write("</head>");
w.write("<body>");
w.write("<a href=\"/index.html\">๋ฉ”์ธ</a>");
w.write("<table>");
w.write("    <thead>");
w.write("    <th>id</th>");
w.write("    <th>username</th>");
w.write("    <th>age</th>");
w.write("    </thead>");
w.write("    <tbody>");
  • ์ž๋ฐ”์ฝ”๋“œ๋กœ HTML์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค ์ฐจ๋ผ๋ฆฌ HTML ๋ฌธ์„œ์— ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋” ํŽธ๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ HTML ๋ฌธ์„œ์— Java ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š” ๋ฐฉ์‹์ด ๋ฐ”๋กœ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ž…๋‹ˆ๋‹ค.
  • ํ…œํ”Œ๋ฆฟ ์—”์ง„์—๋Š” JSP, Thymeleaf, FreeMarker, Velocity ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ ์—”์ง„๋“ค์€ ์žฅ๋‹จ์ ์ด ์žˆ๋Š”๋ฐ ๊ทธ ์ค‘์—์„œ JSP๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋กœ์ง ๊ตฌํ˜„์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

JSP ํŒจํ„ด

JSP(Java Server Page)์€ ํ…œํ”Œ๋ฆฟ ์—”์ง„ ์ค‘ ํ•˜๋‚˜๋กœ JSP๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด build.gradle์— JSP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

//JSP ์ถ”๊ฐ€ ์‹œ์ž‘
	implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.44'
	implementation 'javax.servlet:jstl'
//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>

1. ๋“ฑ๋ก - jsp/members/new-form.jsp

 

<%@ page contentType="text/html;charset=utf-8" language="java" %>

โžฃ ์ฒซ ์ค„์€ JSP๋ฌธ์„œ๋ผ๋Š” ๋œป์œผ๋กœ JSP ๋ฌธ์„œ๋Š” ์ด๋ ‡๊ฒŒ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํšŒ์›๋“ฑ๋ก ํผ JSP๋ฅผ ๋ณด๋ฉด ์ฒซ์ค„์„ ์ œ์™ธํ•˜๊ณค HTML๊ณผ ์™„์ „ํžˆ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค.

 

JSP๋Š” ์„œ๋ฒ„ ๋‚ด๋ถ€์—์„œ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ง  MemberFormSevlet๊ณผ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ฒŒ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

๋‘ ๋ฒˆ์งธ๋Š” ์ €์žฅ ๋กœ์ง์ž…๋‹ˆ๋‹ค.

<%@ page import="hello.servlet.basic.domain.member.Member" %>
<%@ page import="hello.servlet.basic.domain.member.MemberRepository" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    // request, response ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    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. ์ €์žฅ - jsp/members/save.jsp

<%@ page ~~~ %>

โžฃ import๋ฌธ

 

<% ~~ %>

โžฃ ์ด ๋ถ€๋ถ„์— ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

<%= ~~ %>

โžฃ ์ด ๋ถ€๋ถ„์— ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

JSP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— request์™€ response๋Š” ์˜ˆ์•ฝ์–ด๋กœ ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์„ธ ๋ฒˆ์งธ ๋กœ์ง์€ ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ์ž…๋‹ˆ๋‹ค.

<%@ page import="hello.servlet.basic.domain.member.MemberRepository" %>
<%@ page import="hello.servlet.basic.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>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="/index.html">๋ฉ”์ธ</a>
<table>
    <thead>
    <th>id</th>
    <th>username</th>
    <th>age</th>
    </thead>
    <tbody>
    <%
        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>");
        }
    %>
    </tbody>
</table>

3. ์กฐํšŒ - jsp/members.jsp

1. ํšŒ์› ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋จผ์ € ์กฐํšŒ

2. ํšŒ์› ๋ฆฌํฌ์ง€ํ† ๋ฆฌ -> ๋ฆฌ์ŠคํŠธ 

3. <tr><td> HTML ํƒœ๊ทธ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅ 

 

์—ฌ๊ธฐ์„œ out ๋˜ํ•œ JSP๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜ˆ์•ฝ์–ด๋กœ JSP ํŽ˜์ด์ง€๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ• ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์ž…๋‹ˆ๋‹ค.

 

 

<์ฐธ๊ณ  ์ž๋ฃŒ>

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1/dashboard

 

์Šคํ”„๋ง MVC 1ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ•ต์‹ฌ ๊ธฐ์ˆ  - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋•Œ ํ•„์š”ํ•œ ๋ชจ๋“  ์›น ๊ธฐ์ˆ ์„ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ดํ•ดํ•˜๊ณ , ์™„์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํ”„๋ง MVC์˜ ํ•ต์‹ฌ ์›๋ฆฌ์™€ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜๊ณ , ๋” ๊นŠ์ด์žˆ๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž๋กœ ์„ฑ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค., -

www.inflearn.com

https://blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=kimkwon429&logNo=220759230473 

 

 

'๐Ÿ“• Backend > MVC Pattern' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Front-Controller ํŒจํ„ด  (0) 2023.02.27
Spring / MVC ํŒจํ„ด  (0) 2023.02.24
Spring MVC / HttpServletResponse  (0) 2023.02.22
Spring MVC / HttpServletRequest  (0) 2023.02.22
Spring MVC / ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜  (0) 2023.02.21

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ