ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 순수 JSP 예제
    백엔드/SERVLET & JSP 2024. 8. 28. 17:40
    JSP 회원 관리

    JSP 회원 등록 폼

    • new-form.jsp
    <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    <html>
    <head>
        <title> test </title>
    </head>
    <body>
        
        <form action="save.jsp" method="post>
             name: <input type="text" name="username" />
             age: <input type="text" name="age" />
             <button type="submit">전송</button>
        </form>
    
    </body>

     

     

    회원 저장

    • save.jsp
    class Member{
        private Long id;
        private String name;
        private int age;
        
        public Member(String name, int age)
        {
            this.name = name;
            this.age = age;
        }
        
        public void setId(Long id) { this.id = id;}
        public Long getId () { return this.id; }
        
        public void setName(String name) { this.name = name; }
        public String getName() { return this.name; }
        
        public void setAge(int age) {this.age = age;}
        public int getAge() { return this.age;}
    }
    
    
    class MemberRepository{
         
         private final static MemberRepository instance = new MemberRepository();
         
         public static getInstance()
         {
             return this.instance;
         }
         
         private MemberRepository() {}
         
         private Map<Long, Member> store = new HashMap<>();
         private static long sequence = 0L;
         
         public void save(Member member)
         {
            member.setId(++sequence);
            store.put(member.getId(), member);
         }
         
         public List<Member> findAll()
         {
             return new ArrayList<>(store.values());
         }
    }
    
    
    <!-- save.jsp -->
    
    <%@ page import=MemberRepository" %>
    <%@ page import=Member" %>
    <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    
    <%
        MemberRepository memberRepository = MemberRepository.getInstance();
        
        String name = request.getParameter("name");
        int age = request.getParameter("age");
        
        Member member = new Member(name,age);
        memberRepository.save(member);
    %>
    
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        성공
        <ul>
            <li> id = <%= member.getId() %> </li>
            <li> name = <%= member.getName() %> </li>
            <li> age = <%= member.getAge() %> </li>
        </ul>
        <a href="/main"> 메인 </a>
    </body>
    </html>

     

     

    회원 목록

    • members.jsp
    <!-- members.jsp -->
    
    <%@ page import="java.util.List" %>
    <%@ page import="MemberRepository" %>
    <%@ page import="Member" %>
    <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    
    <%
        MemberRepository memberRepository = MemberRepository.getInstance();
        List<Member> members = memberRepository.findAll();
    %>
    
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <a href="/main"> 메인 </a>
        <table>
            <thread>
                <th> id </th>
                <th> name </th>
                <th> age </th>
            </thread>
            <tbody>
            
            <%
                for (Member member: members) {
                    out.write("    <tr>");
                    out.write(".        <td>" + member.getId() + "</td>");
                    out.write(".        <td>" + member.getName() + "</td>");
                    out.write(".        <td>" + member.getAge() + "</td>");
                    out.write(".   </tr>");
                }
            %>
            </tbody>
         </table>
         
    </body>
    </html>

     

     

    한계

    • JSP파일 하나에 비즈니스 로직 및 html (view) 영역이 섞여있다
    • 유지보수가 어렵다
    • 비즈니스 로직이 노출되어있다
    • -> MVC 패턴의 도입 이유

    '백엔드 > SERVLET & JSP' 카테고리의 다른 글

    Spring 예제#1 ( +Servlet, JSP, FrontController )  (0) 2024.08.29
    MVC 패턴 ( Servlet + JSP )  (1) 2024.08.28
    JSP 이론 및 기초 문법  (1) 2024.08.28
    Servlet 예제 ( 회원 저장 / 조회 )  (0) 2024.08.27
    Servlet 응답 처리  (1) 2024.08.27
Designed by Tistory.