IT/Jsp

jsp - 액션태그로 자바빈 객체 생성

노마드오브 2018. 11. 12. 15:32

○ 첫번째 예제


*beanForm.jsp

<%@ 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>Insert title here</title>

</head>

<body>

<h1>회원가입</h1>

<hr>

<form action="beanPro.jsp" method="post">

이름 : <input type="text" name="name"><br>

나이 : <input type="number" name="age" min="0" max="200"><br>

<input type="submit" value="회원가입">

</form>

</body>

</html>



*beanPro.jsp

<%@page import="com.bean.PersonBean"%>

<%@ 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>Insert title here</title>

</head>

<body>

<%

// 한글처리

request.setCharacterEncoding("utf-8");

// name age 파라미터값 가져오기

// String name = request.getParameter("name");

// int age = Integer.parseInt(request.getParameter("age"));

%>

<%-- 이름 : <%=name %> --%>

<%-- 나이 : <%=age %> --%>

<br>

<%

// PersonBean 객체생성

// PersonBean bean = new PersonBean();

// bean.setName(request.getParameter("name"));

// bean.setAge(Integer.parseInt(s))

%>

<%-- 액션태그로 자바빈 객체 생성 --%>

<jsp:useBean id="bean" class="com.bean.PersonBean" />


<%--

<jsp:setProperty property="name" name="bean" param="name" />

<jsp:setProperty property="age" name="bean" param="age" />

--%>


<jsp:setProperty property="*" name="bean" />


<%

// out.println(bean.getName());

%>

<%-- 액션태그 이용해서 자바빈 name age 값 가져와서 출력 --%>

<jsp:getProperty property="name" name="bean" /><br>

<jsp:getProperty property="age" name="bean" /><br>



<%--

자바빈 클래스 com.bean.MyBean 만들기


beanForm2.jsp

아이디 : "id" 

패스워드 : "passwd"

좋아하는 숫자 : "number" (정수형 숫자)

beanPro2.jsp

액션태그 객체생성

액션태그 값 저장

액션태그 값 가져오기 출력

"아이디 : 패스워드 : 좋아하는 숫자 : "

 --%>


</body>

</html>



*PersonBean.java

package com.bean;


// 자바빈 : 데이터를 저장 => 이동전달


// 자바빈 클래스의 필수 구성요소

//1) 기본생성자

//2) getter setter 메소드


public class PersonBean {

// 자바빈 : 데이터를 저장 => 이동 전달

private String name;

private int age;

// getter & setter  메소드

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}





○ 두번째 예제


*beanForm2.jsp

<%@ 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>Insert title here</title>

</head>

<body>


<form action="beanPro2.jsp" method="post">

아이디 : <input type="text" name="id" ><br>

패스워드 : <input type="password" name="passwd" ><br>

좋아하는 숫자 : <input type="number" name="number" min="0" max="200"><br>

<input type="submit" value="전송">

</form>

</body>

</html>



*beanPro2.jsp

<%@ 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>Insert title here</title>

</head>

<body>

<%

request.setCharacterEncoding("utf-8");

%>

<jsp:useBean id="bean" class="com.bean.MyBean" />

<jsp:setProperty property="*" name="bean" />


<jsp:getProperty property="id" name="bean"/><br>

<jsp:getProperty property="passwd" name="bean"/><br>

<jsp:getProperty property="number" name="bean"/><br>

</body>

</html>



*MyBean.java

package com.bean;


public class MyBean {

String id;

String passwd;

int number;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getPasswd() {

return passwd;

}

public void setPasswd(String passwd) {

this.passwd = passwd;

}

public int getNumber() {

return number;

}

public void setNumber(int number) {

this.number = number;

}

}