본문 바로가기

JSP(Java Server Page)

beans를 이용하여 여러개의 데이터를 주고받기

para2_exam.html


<!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>

빈즈로 받게 하기

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

<input type="text" name="me">

<input type="submit" >

</form>

</body>

</html>


==================================================================================================================================

para2_exam.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%

request.setCharacterEncoding("utf-8");

//String name = request.getParameter("name"); // 이런거 x

%>


<!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>


폼빈에 등록된 자료 출력해 보기<br/>

<jsp:useBean id="bean" class="pack.FormBean"/>

<!-- <jsp:setProperty property="name" name="bean"/> --> <!-- 이런거 x -->

<jsp:setProperty property="*" name="bean"/> <!-- 넘어오는 데이터를 모두 받게 된다. name으로 자동 맵핑되어 데이터가 setting된다.-->

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

국어는 <jsp:getProperty property="kor" name="bean"/><br/>

영어는 <jsp:getProperty property="eng" name="bean"/><br/>


<jsp:useBean id="examProcess" class="pack.ExamProcess" />

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

총점은 <jsp:getProperty property="tot" name="examProcess"/>

평균은 <jsp:getProperty property="avg" name="examProcess"/>


</body>

</html>


==================================================================================================================================

FormBean.java


package pack;


public class FormBean {

private String name;

private int kor,eng;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getKor() {

return kor;

}

public void setKor(int kor) {

this.kor = kor;

}

public int getEng() {

return eng;

}

public void setEng(int eng) {

this.eng = eng;

}

}



==================================================================================================================================

ExamProcess.java

package pack;

public class ExamProcess {
private FormBean formBean;
public void setFormBean(FormBean formBean) {
this.formBean = formBean;
}
public int getTot() {
return formBean.getKor() + formBean.getEng();
}
public double getAvg() {
return (formBean.getKor() + formBean.getEng()) / 2.0;
}
}