목차

1. 요구사항 분석

@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;
    }
}
/**
 * 동시성 문제가 고려되어 있지 않음, 실무에서는 ConcurrentHashMap, AtomicLong 사용 고려
 */
public class MemberRepository {
    private static Map<Long, Member> store = new HashMap<>();
    private static long sequence = 0L; //id 하나씩 증가

    //싱글톤으로 생성
    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();
    }
}

2. 서블릿으로 회원 관리 웹 어플리케이션 만들기

@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 {
        //응답으로 http 응답이 나가야함
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");

        //서블릿을 사용하게 되면 자바 코드로 html을 작성해야 하기 때문에 불편하다.
        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");
    }
}

3. JSP이용

//JSP 추가 시작
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'javax.servlet:jstl'
//JSP 추가 끝
<%@ page import="hello.servlet.domain.member.MemberRepository" %>
<%@ page import="hello.servlet.domain.member.Member" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
// request, response 사용 가능
 MemberRepository memberRepository = MemberRepository.getInstance();
 System.out.println("save.jsp");
 String username = request.getParameter("username");
 int age = Integer.parseInt(request.getParameter("age"));
 Member member = new Member(username, age);
 System.out.println("member = " + member);
 memberRepository.save(member);
%>
<html>
<head>
 <meta charset="UTF-8">
</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>

4. MVC패턴