L10Bean
Jsp에서 Bean객체를 사용하는 방법에 대해 알아보자
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>jsp에서 bean객체 사용하기</title> </head> <body> <h1>bean 객체 사용법</h1> <h3><a href="./L01UseBean.jsp">bean에 값을 저장하고 불러오기</a></h3> <h3><a href="./L02Property.jsp">jsp태그를 이용해서 저장하고 불러오기</a></h3> <h3><a href="./L03AddMemberForm.jsp">제출한 양식을 bean으로 받아오기</a></h3> </body> </html> | cs |
Member file 생성
Member file을 생성하고 private로 사용할 변수 설정 후 Generate getters and setters로 모든 항목을 생성해준다.
예제 1. Member.java
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.jsp.bean; //Member는 데이터베이스의 Member table과 매칭 public class Member { private int num; private String id; private String pwd; private String name; private String email; private String phone; private char admin; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public char getAdmin() { return admin; } public void setAdmin(char admin) { this.admin = admin; } } | cs |
L01UseBean.jsp에서 bean에 값을 저장하고 불러오기
예제 2. L01UseBean.jsp
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Import와 UseBean의 차이</title> </head> <body> <jsp:useBean id="bean" class="com.jsp.bean.Member" /> <h1>Java Bean 객체 생성</h1> <% //Member bean = new Member(); bean.setId("jspLesson"); bean.setName("kjs"); bean.setPhone("010-1111-2222"); out.print("<h3>Bean객체 생성완료</h3>"); %> <hr> <h1>bean 출력</h1> <p><strong>id: <%=bean.getId() %></strong></p> <p><strong>name: <%=bean.getName() %></strong></p> <p><strong>phone: <%=bean.getPhone() %></strong></p> </body> </html> | cs |
L02Property.jsp에서 jsp 태그를 이용하여 저장하고 불러오는 방법을 알아보자
예제 3. L02Property.jsp
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Property를 이용해서 Bean 설정하기</title> </head> <body> <jsp:useBean id="member" class="com.jsp.bean.Member" /> <h1>jsp property로 Member Bean 설정하기</h1> <jsp:setProperty property="id" name="member" value="jspLesson"/> <!-- member.setId("jspLesson") FrontEnd 개발자가 java 구문을 싫어해서 ->xml 형식이 더 편하다. --> <jsp:setProperty property="name" name="member" value="kjs"/> <jsp:setProperty property="email" name="member" value="kjs@gmail.com"/> <jsp:setProperty property="phone" name="member" value="010-1234-5678"/> <h3>설정완료</h3> <hr> <h1>jsp property로 Member Bean 가져오기</h1> <h3><jsp:getProperty property="id" name="member"/></h3> <h3><jsp:getProperty property="name" name="member"/></h3> <h3>${member.email}</h3> <h3>${member["phone"]}</h3> </body> </html> | cs |
L03AddMemberForm.jsp에서 제출한 양식을 bean으로 받아오기
예제 4. L03AddMemberForm.jsp
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>bean으로 parameter 받기</title> </head> <body> <h1>회원가입 양식</h1> <form action="L04DetailMember.jsp" method="post"> <p> <label for="uNum">num :</label> <input type="text" id="uNum" name="num" value="1"> <!-- type="hidden" 눈에 보이지는 않지만 파라미터 값을 넘길 때 사용--> </p> <p> <label for="uName">이름 :</label> <input type="text" id="uName" name="name" size="20" value="kjs"> </p> <p> <label for="uId">ID :</label> <input type="text" name="id" value="jspLesson"> </p> <p> <label for="uPass">Password :</label> <input type="password" id="uPass" name="pwd" size="20" value="admin1234"> </p> <p> <label for="uEmail">E-mail :</label> <input type="email" id="uEmail" name="email" size="20" value="kjs@gmail.com"> </p> <p> <label for="uPhone">Phone :</label> <input type="text" id="uPhone" name="phone" size="20" value="010-1234-5678"> </p> <p> <label for="admin">관리자 </label> <input type="radio" id="admin" name="admin" value="1" checked="checked"> <label for="nomal">일반 </label> <input type="radio" id="nomal" name="admin" value="0" > </p> <input type="submit" value="제출"> </form> </body> </html> | cs |
예제 5. L04DetailMember.jsp
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 32 33 34 35 36 37 38 39 40 41 42 | <%@page import="com.jsp.bean.Member"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Bean으로 parameter 받기 </title> </head> <jsp:useBean id="mem" class="com.jsp.bean.Member" /> <jsp:setProperty property="*" name="mem"/> <body> <%-- <% Member mem = new Member(); mem.setId(request.getParameter("id")); mem.setName(request.getParameter("name")); mem.setEmail(request.getParameter("email")); mem.setPhone(request.getParameter("phone")); mem.setPwd(request.getParameter("pwd")); mem.setAdmin(request.getParameter("admin").charAt(0)); %>--%> <h1>입력 받은 회원 정보</h1> <table> <tr><td>아이디</td> <td><%=mem.getId()%></td> </tr> <tr><td>이름</td> <td><%=mem.getName()%></td> </tr> <tr><td>비밀번호</td> <td><%=mem.getPwd()%></td> </tr> <tr><td>Email</td> <td><%=mem.getEmail()%></td> </tr> <tr><td>Phone</td> <td><%=mem.getPhone()%></td> </tr> </table> </body> </html> | cs |
'IT story > JSP' 카테고리의 다른 글
[Jsp 강의] L12JDBC (0) | 2016.10.28 |
---|---|
[Jsp 강의] L11ELtag (0) | 2016.10.27 |
[Jsp 강의] L09Cookie (0) | 2016.10.27 |
[Jsp 강의] L08Session (0) | 2016.10.27 |
[Jsp 강의] L07Template (0) | 2016.10.27 |