티스토리 뷰

백엔드/SERVLET & JSP

순수 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 패턴의 도입 이유
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함