chap03 JSP 기본 구조

4. 표준 액션태크

4.1 include

4.1.1 디렉티브 include

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

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 계산기 프로그램에 메뉴 달기

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>

4.2 페이지 이동

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

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

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 활용

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>