JSP
[쉽게 배우는 JSP 웹 프로그래밍] 4장 연습문제
코딩 못하는 감자
2024. 4. 4. 16:05
1. 1
2. 2
3. 3
4. 1
5. 2
6. 1
7. 2
8번
forward.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Gugudan</title>
</head>
<body>
<h4>구구단 출력하기</h4>
<jsp:forward page="include_data.jsp">
<jsp:param name="data" value="<%=13%>" />
</jsp:forward>
</body>
</html>
forward_data.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Gugudan</title>
</head>
<body>
<%
int n=Integer.parseInt(request.getParameter("data"));
for(int i=1; i<10; i++){
out.println(n+"*"+i+"="+n*i+"<br>");
}
%>
</body>
</html>
9번
include.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Gugudan</title>
</head>
<body>
<h4>구구단 출력하기</h4>
<jsp:include page="include_data.jsp">
<jsp:param name="data" value="<%=13%>" />
</jsp:include>
</body>
</html>
include_data.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Gugudan</title>
</head>
<body>
<%
int n=Integer.parseInt(request.getParameter("data"));
for(int i=1; i<10; i++){
out.println(n+"*"+i+"="+n*i+"<br>");
}
%>
</body>
</html>
10번
useBean.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Action Tag</title>
</head>
<body>
<h4>구구단 출력하기</h4>
<jsp:useBean id="bean" class="ch04.com.dao.GuGuDan" /> <%--GuGuDan클래스의 bean객체 생성--%>
<%
bean.setN(5); // 구구단 단수 set
for(int i=1; i<10; i++){ // 1부터 9까지
out.println(bean.getN()+"*"+i+"="+bean.process(i)+"<br>"); // 구구단 출력
}
%>
</body>
</html>
GuGuDan.java
package ch04.com.dao;
public class GuGuDan {
private int n; // 구구단 n단
public GuGuDan(){ // 기본 생성자
}
public int getN() { // getter
return n;
}
public void setN(int n) { //setter
this.n = n;
}
public int process(int i) { // 곱셈 반환
return n*i;
}
}
forward 태그와 include 태그의 차이점
->제어권이 어디있는가
forward 태그인 경우 url1에서 forward해서 url2로 전달하게 되면(제어권을 넘겨주면) url1이 제어권을 다시 돌려받지 못한다.
그에 반해 include 태그에서는 url2에서 처리를 완료하면 url1으로 제어권을 돌려준다.
따라서 클라이언트화면에 url1의 내용속에 url2가 들어간 것처럼 보여진다.