chap03 JSP 기본 구조
4. 표준 액션태크
- JSP에 특별한 기능 제공
- jsp:include
- jsp:forward
- jsp:useBean, jsp:setProperty, jsp:getProperty
4.1 include
- 공통으로 사용하는 페이지 처리
4.1.1 디렉티브 include
- <%@ include file="path">
index.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>
<%@ include file="menu.jsp" %>
<h1>계산기 사이트 입니다.</h1>
<h2>메뉴를 선택하세요</h2>
</body>
</html>
4.1.2 액션태그 include
- <jsp:include page="path" />
index.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>
<jsp:include page="menu.jsp" />
<h1>계산기 사이트 입니다.</h1>
<h2>메뉴를 선택하세요</h2>
</body>
</html>
4.1.3 계산기 프로그램에 메뉴 달기
- 4칙연산을 각 메뉴로 바꾸기
menu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<hr>
<a href="index.jsp">HOME</a> |
<a href="add.jsp">더하기</a> |
<a href="sub.jsp">빼기</a> |
<a href="mul.jsp">곱하기</a> |
<a href="div.jsp">나누기</a>
<hr>
add.jsp
<%@page import="chap03.CalService"%>
<%@page import="chap03.CalVO"%>
<%@ 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>
<%
CalVO vo = null;
CalService cs = null;
String sA = request.getParameter("a");
String sB = request.getParameter("b");
String sel = "+";
int a = 0, b=0, result = 0;
if(sA != null && sel != null){
a = Integer.parseInt(sA);
b = Integer.parseInt(sB);
vo = new CalVO(a,b,result,sel);
cs = new CalService();
vo = cs.cal(vo);
}else {
vo = new CalVO();
}
%>
<%@ include file="menu.jsp" %>
<h1>
4칙연산 계산기<br>
<form action="add.jsp" method="post">
<input type="text" name="a">
+
<input type="text" name="b"> <br>
<input type="submit" value="계산하기">
<hr>
<h1>덧셈 결과</h1>
<hr>
<h2>
<%=vo.getA() %> <%=vo.getSel() %> <%=vo.getB() %> = <%=vo.getResult() %>
</h2>
</body>
</html>
- sub.jsp, mul.jsp, div.jsp 작업
4.2 페이지 이동
- 페이지 자동 이동 처리
- response.sendRedirect("path)
- <jsp:forward page="path">
4.2.1 Response 객체 sendRedirect()
- 주소 노출 됨
index.jsp
<%@ include file="menu_send.jsp" %>
menu_send.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<hr>
<a href="index.jsp">HOME</a> |
<a href="add_send.jsp">더하기</a> |
<a href="sub_send.jsp">빼기</a> |
<a href="mul_send.jsp">곱하기</a> |
<a href="div_send.jsp">나누기</a>
<hr>
add_send.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String sA = "10";
String sB = "20";
String param = "a="+sA+"&b="+sB;
String path = "add.jsp?"+param;
response.sendRedirect(path);
%>
4.2.2 액션 태그 <jsp:forward page="path">
- 주소 노출 안됨
- request Scope 값 사용 가능
index.jsp
<%@ include file="menu_forward.jsp" %>
mene_forward.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<hr>
<a href="index.jsp">HOME</a> |
<a href="add_forward.jsp">더하기</a> |
<a href="sub_forward.jsp">빼기</a> |
<a href="mul_forward.jsp">곱하기</a> |
<a href="div_forward.jsp">나누기</a>
<hr>
add_forward.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String sA = "10";
String sB = "20";
String param = "a="+sA+"&b="+sB;
String path = "add.jsp?"+param;
%>
<jsp:forward page="<%=path %>" />
4.3 액션 태그 jsp:useBean
- class 인스턴스 생성, 기본 생성자 필요
- id, class, scope 속성 사용
- <jsp:setProperty property="" value=""> : 속성 값 set
- <jsp:getProperty property=""> : 속성 값 get
4.3.1 액션 태그 jsp:useBean 기본 사용
calvo_use.jsp
<%@page import="chap03.CalVO"%>
<%@ 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>
<h1>NEW</h1>
<%
CalVO vo1 = new CalVO();
%>
<jsp:useBean id="vo2" class="chap03.CalVO"/>
<h1>SET</h1>
<%
vo1.setA(10);
vo1.setB(10);
vo1.setSel("+");
%>
<jsp:setProperty name="vo2" property="a" value="10" />
<jsp:setProperty name="vo2" property="b" value="20" />
<jsp:setProperty name="vo2" property="sel" value="+" />
<h1>GET</h1>
<h2>
A: <%=vo1.getA() %>,
B: <%=vo1.getB() %>,
SEL: <%=vo1.getSel() %>
<hr>
A: <jsp:getProperty name="vo2" property="a" />,
B: <jsp:getProperty name="vo2" property="b" />,
SEL :<jsp:getProperty name="vo2" property="sel" />,
</h2>
</body>
</html>
4.3.2 액션 태그 jsp:useBean 활용
- 파라미터 값 자동 저장
- <jsp:setProperty name="vo" property="*" />
cal_useBean.jsp
<%@page import="chap03.CalService"%>
<%@page import="chap03.CalVO"%>
<%@ 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>
<jsp:useBean id="vo" class="chap03.CalVO"/>
<jsp:setProperty name="vo" property="*" />
<jsp:useBean id="cs" class="chap03.CalService"/>
<%
vo = cs.cal(vo);
%>
<h1>
4칙연산 계산기<br>
<form action="cal_useBean.jsp" method="post">
<input type="text" name="a">
<select name="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="b"> <br>
<input type="submit" value="계산하기">
<hr>
<h1>계산결과</h1>
<hr>
<h2>
<%=vo.getA() %> <%=vo.getSel() %> <%=vo.getB() %> = <%=vo.getResult() %>
</h2>
</body>
</html>