@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;
}
}
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 clearStore() {
store.clear();
}
}
@WebServlet(name = "memberSaveServlet", urlPatterns = "/servlet/members/save")
public class MemberSaveServlet extends HttpServlet {
MemberRepository memberRepository = MemberRepository.getInstance();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
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);
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>");
}
}
템플릿 엔진
을 통해 동적으로 HTML을 만들 수 있음<%@ page import="hello.servlet.domain.member.MemberRepository" %>
<%@ page import="hello.servlet.domain.member.Member" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
MemberRepository memberRepository = MemberRepository.getInstance();
System.out.println("MemberSaveServlet.service");
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"/>
<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>
상위 절반
하위 절반
JSP가 너무 많은 역할을 함
MVC 패턴의 등장
비지니스 로직은 서블릿
에서 처리
JSP
는 목적에 맞게 화면을 그리는 일에만 집중하도록 함
모델
뷰
컨트롤러
컨트롤러는 비지니스 로직(service)를 호출하여 실행하는 방식
비지니스 로직이 변경되면 컨트롤러도 변경될 수 있음
모델
뷰
컨트롤러
@WebServlet(name = "mvcMemberSaveServlet", urlPatterns = "/servlet-mvc/members/save")
public class MvcMemberSaveServlet extends HttpServlet {
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);
// 모델에 비지니스 로직 실행 결과 저장
request.setAttribute("member", member);
// 뷰로 모델을 넘겨줌
String viewPath = "/WEB-INF/views/save-result.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
WEB-INF
forward()
redirect()
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
성공
<ul>
<li>id=${member.id}</li>
<li>username=${member.userName}</li>
<li>age=${member.age}</li>
</ul>
<a href="/index.html">메인</a>
</body>
</html>
${member.id}
or ${member.getId}
⇒ 기능이 복잡해질수록 공통으로 처리
해야할 부분이 많아짐
front-controller 패턴
을 도입해 이런 문제를 해결함spring mvc의 핵심!