[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;
          }
      }
    
  • μ‹€ν–‰κ²°κ³Ό

Categories:

JSP/Servlet