-
웹 애플리케이션으로 Servlet / JSP / 고전 MVC 패턴 비교 - ServletSpring/MVC1-Servlet vs JSP vs MVC패턴 2023. 6. 8. 17:10728x90
JSP Pattern
https://hongs429-blog.tistory.com/43
웹 애플리케이션으로 Servlet / JSP / 고전 MVC 패턴 비교 - JSP
Servlet Pattern https://hongs429-blog.tistory.com/42 웹 애플리케이션으로 Servlet / JSP / 고전 MVC 패턴 비교 - Servlet 간단한 회원 관리 웹 애플리케이션 구현( Servlet ) 예전의 Servlet 구조화 JSP, MVC 패턴의 비교를
hongs429-blog.tistory.com
고전MVC Pattern
https://hongs429-blog.tistory.com/44
웹 애플리케이션으로 Servlet / JSP / 고전 MVC 패턴 비교 - MVC
Servlet Pattern https://hongs429-blog.tistory.com/42 웹 애플리케이션으로 Servlet / JSP / 고전 MVC 패턴 비교 - Servlet 간단한 회원 관리 웹 애플리케이션 구현( Servlet ) 예전의 Servlet 구조화 JSP, MVC 패턴의 비교를
hongs429-blog.tistory.com
간단한 회원 관리 웹 애플리케이션 구현( Servlet )
예전의 Servlet 구조화 JSP, MVC 패턴의 비교를 해보자
그 중에서 이번에는 Servlet으로 회원관리 웹을 만들어 볼 것이다.
회원 정보
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; } }
기능
- 회원 가입
- 회원 목록 조회
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; 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()); } }
이제 Servlet을 이용한 웹 어플리케이션을 본격적으로 만들 것이다.
Servlet 으로 웹의 요청을 받아 페이지를 내려주려면 request 와 response를 하나의 파일에서 처리를 해주어야 한다.
회원 가입 기능 구현 및 페이지
1. 회원 가입 폼 페이지
import hello.servlet.domain.member.MemberRepository; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @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 writer = response.getWriter(); writer.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"); } }
2. 회원 가입(저장)
import hello.servlet.domain.member.Member; import hello.servlet.domain.member.MemberRepository; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "memberSaveServlet", urlPatterns = "/servlet-mvc/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>"); } }
3. 회원 목록 조회
import hello.servlet.domain.member.Member; import hello.servlet.domain.member.MemberRepository; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.net.http.HttpClient; import java.util.List; @WebServlet(name = "memberListServlet", urlPatterns = "/servlet/members") public class MemberListServlet extends HttpServlet { private MemberRepository memberRepository = MemberRepository.getInstance(); 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>"); } }
요약
Servlet만으로 페이지를 구현한다는 것은 순수 자바로 HTML을 만들어주는 작업이 필요했다.
때문에 response에 담아서 보내주어야 하는 HTML코드를 구현하는 과정이 매우 번거로웠다.
'Spring > MVC1-Servlet vs JSP vs MVC패턴' 카테고리의 다른 글
(3) MVC 프레임워크 만들기 - Controller 구현체들의 공통로직 처리-2 (0) 2023.06.13 (2) MVC 프레임워크 만들기 - Controller 구현체들의 공통로직 처리 (0) 2023.06.13 (1) MVC 프레임워크 만들기 - FrontController의 도입 (0) 2023.06.13 웹 애플리케이션으로 Servlet / JSP / 고전 MVC 패턴 비교 - MVC (0) 2023.06.08 웹 애플리케이션으로 Servlet / JSP / 고전 MVC 패턴 비교 - JSP (0) 2023.06.08