[JSP] μλ°λΉ(JavaBean)
μλ° λΉ(JavaBean)
-
μλ° μΈμ΄μ λ°μ΄ν°(μμ±)μ κΈ°λ₯(λ©μλ)μΌλ‘ μ΄λ£¨μ΄μ§ ν΄λμ€
-
μλ°λΉμ μ΄μ©νλ©΄ JSP νμ΄μ§μ λμμΈ λΆλΆκ³Ό λΉμ¦λμ€ λ‘μ§ λΆλΆμ λλμΌλ‘μ¨ λ³΅μ‘ν μλ°μ½λλ₯Ό μ€μ΄κ³ νλ‘κ·Έλ¨μ μ¬μ¬μ©μ±μ μ¦κ°μν¬ μ μλ€.
-
μλ°λΉ κ·μΉ
-
λν΄νΈ μμ±μ μμ΄μΌ ν¨ (맀κ°λ³μ X)
-
λ©€λ²λ³μ(= Property)λ§λ€ λ³λμ get/set λ©μλ μ‘΄μ¬ν΄μΌ ν¨
-
get λ©μλλ 맀κ°λ³μ X, set λ©μλλ νλ μ΄μμ 맀κ°λ³μ
-
λ©€λ²λ³μμ μ κ·Όμ μ΄μλ private, set/get λ©μλ μ κ·Όμ μ΄μλ public, ν΄λμ€μ μ κ·Όμ μ΄μλ public
-
-
μλ°λΉ μ‘μ νκ·Έ
-
<jsp:useBean id="μΈμ€ν΄μ€ μ΄λ¦" class="ν¨ν€μ§λͺ .μλ°λΉν΄λμ€(.java μλ΅)" scope="μ¬μ©λ²μ"/>
-
μλ° κ°μ²΄ μ±μ±
-
scope : μλ°λΉμ λ²μ μ€μ
-
page : νμ¬ νμ΄μ§μμλ§ μ μ§
-
request : requestλ₯Ό μμ² λ°κ³ μ²λ¦¬ λ λ κΉμ§λ§ μ μ§
-
session : sessionμ΄ μ μ§λλ λμ μ μ§
-
application : μ¬μ΄νΈ μ 체 λ²μ, μλ² μ’ λ£ μ κΉμ§ κ³μ μ μ§
-
-
-
<jsp:setProperty name="" property="" value=""/>
-
μμ±λ κ°μ²΄μ setter λ©μλ νΈμΆ
-
name : useBean μ‘μ νκ·Έλ‘ μμ±λ κ°μ²΄μ μ΄λ¦ (= λΉ μ΄λ¦)
-
property : μμ±, λ©€λ²λ³μ, νλ / β * β : κΈ°λ³Έκ° μ€μ
-
value : μμ±(λ°μ΄ν°) κ°
-
-
<jsp:getProperty name="" property=""/>
- μμ±λ κ°μ²΄μ getter λ©μλ νΈμΆ
-
μμ λ₯Ό ν΅ν΄ λΉ μ²λ¦¬κ³Όμ μμ보기
-
login.html
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>λ‘κ·ΈμΈ νλ©΄</title> <style> div#wrap { width: 400px; padding: 20px; border: 1px solid #000; } form, input, button { font-size: 20px; padding: 4px; } span { width: 40px; display: inline-block; } td{ padding: 6px; } button { width: 90px; height: 90px; } </style> </head> <body> <div id="wrap"> <h1>λ‘κ·ΈμΈ νλ©΄</h1> <form action="Login_Proc.jsp"> <table> <tr> <td> <span>ID</span> <input type="text" name="uid" size="10"/></td> <td rowspan="2"><button>λ‘κ·ΈμΈ</button></td> </tr> <tr> <td> <span>PW</span> <input type="password" name="upw" size="10"/></td> </tr> </table> </form> </div> </body> </html>
-
pack_Login.Login_Bean.java
package pack_Login; public class Login_Bean { private String uid; private String upw; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUpw() { return upw; } public void setUpw(String upw) { this.upw = upw; } }
-
Login_Proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); String uid = request.getParameter("uid"); String upw = request.getParameter("upw"); %> <!-- λΉκ³Ό μ°κ²° --> <jsp:useBean class="pack_Login.Login_Bean" id="loginbean" scope="page"></jsp:useBean> <jsp:setProperty name="loginbean" property="uid" value="<%=uid %>"/> <jsp:setProperty name="loginbean" property="upw" value="<%=upw %>"/> <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>ν μ€νΈ</title> <style> div{ font-size: 24px; } </style> </head> <body> <div id="wrap"> <h2>λ‘κ·ΈμΈ μ 보</h2> <div> <h3>μμ΄λ</h3> <jsp:getProperty property="uid" name="loginbean"/> <h3>λΉλ°λ²νΈ</h3> <jsp:getProperty property="upw" name="loginbean"/> </div> <button type="button" onclick="history.back()">λ‘κ·ΈμΈ νλ©΄μΌλ‘ λμκ°κΈ°</button> </div> </body> </html>
!! λΉ νλμ μ΄λ¦κ³Ό μ λ¬νκ³ μ νλ 맀κ°λ³μμ μ΄λ¦μ΄ κ°λ€λ©΄? (browser parameter name = bean field name) !!
-
request.getParameter() μ μ νμ μμ
-
jsp:setProperty μ‘μ νκ·Έμ property, value νλνλ μ μ νμ μμ΄
<jsp:setProperty name="" property="*" />
μ κ°μ΄ μμ± κ°λ₯ -
μμ Login_Proc.jsp νμΌμ κ°λ¨νκ² λ°κΏλ³΄μ
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); %> <jsp:useBean class="pack_Login.Login_Bean" id="loginbean" scope="page"></jsp:useBean> <jsp:setProperty name="loginbean" property="*" /> <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href=""> <style> div{ font-size: 24px; } </style> </head> <body> <div id="wrap"> <h2>λ‘κ·ΈμΈ μ 보</h2> <div> <h3>μμ΄λ</h3> <jsp:getProperty property="uid" name="loginbean"/> <h3>λΉλ°λ²νΈ</h3> <jsp:getProperty property="upw" name="loginbean"/> </div> <button type="button" onclick="history.back()">λ‘κ·ΈμΈ νλ©΄μΌλ‘ λμκ°κΈ°</button> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src=""></script> </body> </html>
λΉμ μ΄μ©ν νμκ°μ νμ΄μ§
-
BeanEx.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" lang="ko"> <title>νμκ°μ νΌ</title> <style type="text/css"> div#wrap { width: 300px; margin: 0 auto; border: 1px solid black; padding: 20px } h1 { text-align: center; } .btn { background-color: #fff; border: 1px solid black; } </style> </head> <body> <div id="wrap"> <h1>νμκ°μ </h1> <form action="JoinBean.jsp" method="post"> μμ΄λ : <input type="text" name="uid" id="uid"> <br><br> λΉλ°λ²νΈ : <input type="password" name="upw" id="upw"> <br><br> μ΄λ¦ : <input type="text" name="name" id="name"> <br><br> μλ μμΌ : <input type="date" name="birthdate" id="birthdate"> <br><br> μ£Όμ : <input type="text" name="addr" id="addr"> <br><br> μ΄λ©μΌ : <input type="email" name="email" id="email"> <br><br> <input type="submit" value="κ°μ νκΈ°" class="btn"> <input type="reset" value="λ€μμ λ ₯" class="btn"> </form> </div> </body> </html>
-
JoinBean.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); %> <jsp:useBean id="join" class="join.CustomerInfo" scope="page"></jsp:useBean> <jsp:setProperty property="*" name="join"></jsp:setProperty> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" lang="ko"> <title>νμκ°μ κ²°κ³Ό</title> </head> <body> <h1>νμ μ 보</h1> μμ΄λ : <%=join.getUid() %> <br> λΉλ°λ²νΈ : <%=join.getUpw() %> <br> μ΄λ¦ : <%=join.getName() %> <br> μλ μμΌ : <%=join.getBirthdate() %> <br> μ£Όμ : <%=join.getAddr() %> <br> μ΄λ©μΌ : <%=join.getEmail() %> </body> </html>
-
CustomerInfo.java
package join; public class CustomerInfo { private String uid; private String upw; private String name; private String birthdate; private String addr; private String email; public CustomerInfo() { } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUpw() { return upw; } public void setUpw(String upw) { this.upw = upw; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthdate() { return birthdate; } public void setBirthdate(String birthdate) { this.birthdate = birthdate; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
-
μ€νκ²°κ³Ό