본문 바로가기

JSP/useBean05 indexed property

useBean05 indexed property

브라우저에서 전달된 배열을 빈 객체에 전달하고 빈 객체로부터 가져오는 예

form.jsp
.................................................................................................................................................................................................................................................

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>한개의 파라미터 이름으로 다수개의 값을 전달하는 폼 예제</title>
</head>
<body>
<br><center>
<form method="post" action="useBean.jsp">
 이름 <input type="text" name="name"><br>
 전화 <input type="text" name="phone"><br>
 메일 <input type="text" name="email"><br>
 
 취미 <br>
 <input type="checkbox" name="hobby" value="movie">영화
 <input type="checkbox" name="hobby" value="game">게임
 <input type="checkbox" name="hobby" value="travel">여행
 <input type="checkbox" name="hobby" value="reading">독서
 <input type="submit" value="저 장">
</form>
</center>
</body>
</html>



.................................................................................................................................................................................................................................................
useBean.jsp
.................................................................................................................................................................................................................................................

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="admin.*" %>

<% request.setCharacterEncoding("KSC5601"); %>
<jsp:useBean id="emp" class="admin.Employee"/>
<jsp:setProperty property="*" name="emp"/>

<html>
<head>
<title>서버측에서 폼 데이터를 받아서 빈 객체에 할당하는 예</title>
</head>
<body>
이름: <jsp:getProperty property="name" name="emp"/><br>
전화: <jsp:getProperty property="phone" name="emp"/><br>
메일: <jsp:getProperty property="email" name="emp"/><br>
취미: <%-- getProperty액션을 이용하여 indexed Property를 다룰 수 없다 --%>
 <%
  String[] hobby = emp.getHobby();
  for(int i=0;i<hobby.length;i++) {   %>
   <%= hobby[i] %>
 <%}%><br>

</body>
</html>



.................................................................................................................................................................................................................................................
Employee.java
.................................................................................................................................................................................................................................................

package admin;

public class Employee {
 
 private String name;
 private String phone;
 private String email;
 private String[] hobby;

 public Employee() {}

 public String[] getHobby() {
  return hobby;
 }

 public void setHobby(String[] hobby) {
  this.hobby = hobby;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPhone() {
  return phone;
 }

 public void setPhone(String phone) {
  this.phone = phone;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }
 
 /*아래처럼 배열의 원소를 하나씩 리턴해 주는 메소드를 만들고 JSP에서 호출할 수도 있다*/
 public String getHobby(int idx){
  return hobby[idx];
 }
}