servlet(.java) -> jsp -> servlet(.java)
확장자는 *.java 이며 컴파일해서 .class형태로 배포한다.
빈즈는 dll과 같은 원리를 가지고 있다.
빈즈의 종류
DTO(Data Transfer Object) class
Manager(Process) class
DAO(Data Access Object) class
DTO (Data Transfer Object) 데이터 전송 객체
DAO (Data Access Object)
직접 해보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>computeForm.jsp</title>
</head>
<body>
<h3>* 계산기 *</h3>
<!-- 1) JSP 메소드
<form action="computeok1.jsp">
-->
<!-- 2) new 연산자
<form action="computeok2.jsp">
-->
<!-- 3) Jave Bean -->
<form action="computeok3.jsp">
<table border="1">
<tr>
<th>숫자1</th>
<td><input type="number" name="num1" size="5"></td>
</tr>
<tr>
<th>숫자2</th>
<td><input type="number" name="num2" size="5"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="계산">
</td>
</tr>
</table>
</form>
</body>
</html>
JSP 메소드를 이용한 계산
computeok1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>computeOK1</title>
</head>
<body>
<h3>계산결과</h3>
<h4>메소드 호출</h4>
<%
int num1=Integer.parseInt(request.getParameter("num1").trim());
int num2=Integer.parseInt(request.getParameter("num2").trim());
out.print(num1+"+" +num2 +"="+add(num1,num2));
out.print("<hr>");
out.print(num1+"-" +num2 +"="+sub(num1,num2));
out.print("<hr>");
out.print(num1+"*" +num2 +"="+mul(num1,num2));
out.print("<hr>");
out.print(num1+"/" +num2 +"="+div(num1,num2));
out.print("<hr>");
out.print(num1+"%" +num2 +"="+mod(num1,num2));
out.print("<hr>");
%>
<%!
//jsp 작성 영역
public int add(int a, int b){return a+b;}
public int sub(int a, int b){return a-b;}
public int mul(int a, int b){return a*b;}
public int div(int a, int b){return a/b;}
public int mod(int a, int b){return a%b;}
%>
</body>
</html>
new 연산자를 이용해서 객체를 생성한 후 메소드 호출
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="net.testbean.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>computeok2</title>
</head>
<body>
<h3>계산 결과</h3>
<h4>new 연산자를 이용해서 객체 생성후 메소드 호출</h4>
<%
int num1=Integer.parseInt(request.getParameter("num1").trim());
int num2=Integer.parseInt(request.getParameter("num2").trim());
//객체 생성
//-> Ram의 heap영역에 메모리가 할당되는 것
Compute com=new Compute();
out.print(num1+"+" +num2 +"="+ com.add(num1,num2));
out.print("<hr>");
out.print(num1+"-" +num2 +"="+com.sub(num1,num2));
out.print("<hr>");
out.print(num1+"*" +num2 +"="+com.mul(num1,num2));
out.print("<hr>");
out.print(num1+"/" +num2 +"="+com.div(num1,num2));
out.print("<hr>");
out.print(num1+"%" +num2 +"="+com.mod(num1,num2));
out.print("<hr>");
%>
</body>
</html>
JAVA Bean클래스 작성
package net.testbean;
public class Compute {
public int add(int a, int b){return a+b;}
public int sub(int a, int b){return a-b;}
public int mul(int a, int b){return a*b;}
public int div(int a, int b){return a/b;}
public int mod(int a, int b){return a%b;}
}
Jave Bean을 이용한 계산
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="net.testbean.*" %>
<jsp:useBean id="comp" class="net.testbean.Compute" scope="page"></jsp:useBean>
<%--
● [액션태그]
<jsp:useBean id=""></jsp:useBean> 객체생성(Object)
<jsp:forward page=""></jsp:forward> 페이지 이동
<jsp:include page=""></jsp:include> 공통 페이지 가져오기
● [Java Bean 객체 생성]
<jsp:useBean id="객체명"
class="팩키지명.클래스명"
scope="page"></jsp:useBean>
● [Scope 유효범위]
- scope="page" 현재 페이지에서만 유효. 생략시 기본값
- scope="request" 다른 페이지에서 공유
- scope="session" 다른 페이지에서 공유
- scope="application" 다른 페이지에서 공유
● [주의사항]
- Java Bean(id)가 제대로 생성 안될 수도 있음.
해당프로젝트 우클릭 Refresh
Validate
Project -> Clean하고 테스트할것!!
--%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>computeok3.jsp</title>
</head>
<body>
<h3>* 계산 결과 *</h3>
<h4>3) Java Bean을 이용해서 객체 생성후 메소드 호출</h4>
<%
int num1=Integer.parseInt(request.getParameter("num1").trim());
int num2=Integer.parseInt(request.getParameter("num2").trim());
out.print(num1 + "+" + num2 + "=" + comp.add(num1,num2));
out.print("<hr>");
out.print(num1 + "-" + num2 + "=" + comp.sub(num1,num2));
out.print("<hr>");
out.print(num1 + "*" + num2 + "=" + comp.mul(num1,num2));
out.print("<hr>");
out.print(num1 + "/" + num2 + "=" + comp.div(num1,num2));
out.print("<hr>");
out.print(num1 + "%" + num2 + "=" + comp.mod(num1,num2));
out.print("<hr>");
%>
</body>
</html>
calcform.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="calcok.jsp">
<table border="1">
<tr>
<th>숫자</th>
<td><input type="number" name="num" size="5"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="계산">
</td>
</tr>
</table>
</form>
</body>
</html>
CalcBean.java
package net.testbean;
public class CalcBean {
public int abs(int su){ //절대값 구하기
return Math.abs(su);
}//abs() end
public long fact(int su){ //팩토리얼값 구하기
return (su==0) ? 1 : su*fact(su-1);
}//fact() end
}//class end
calcok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="calc" class="net.testbean.Calcbean" scope="page"></jsp:useBean>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>절대값 팩토리얼 구하기</title>
</head>
<body>
<%
int num=Integer.parseInt(request.getParameter("num").trim());
out.print(calc.abs(num));
out.print("<hr>");
out.print(calc.fact(num));
out.print("<hr>");
%>
</body>
</html>