Please Enable JavaScript!
Mohon Aktifkan Javascript![ Enable JavaScript ]

JSP 실습 6 (include 태그)

2010. 4. 9. 14:14programming/jsp

728x90

 

6. 위의 그림에서 처럼 include_1.jsp에서 입력 받은 내용이 include_2.jsp에서 출력되도록
 

include 액션 태그를 사용하여 jsp페이지를 작성하시오.

 include_1.jsp – 이름 입력
 include_2.jsp – include 객체를 사용하여 정보를 include_3.jsp로 전달
 include_3.jsp – 결과를 출력

<%-- include_1.jsp --%>

<%@page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Insert title here</title>

</head>
<body>

include 액션태그 1
<hr width = 300 align=left></hr>
<form action = include_2.jsp method=post>
<br>
이름을 입력하면 "이름님 반갑습니다"가 출력됩니다.
<br>
<br>
<input type=text name=name size=20></input>
<input type=submit value="확인"></input>
</form>
</body>
</html>
<%-- include_2.jsp 페이지에 form 태그를 이용해서  name 데이터를 보낸다. --%>


<%-- include_2.jsp --%>

<%@page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<HTML>
<HEAD><TITLE>include_2.jsp</TITLE>

</HEAD>
<BODY>
<% request.setCharacterEncoding("euc-kr");%>
<%-- 한글 처리를 해주기 위함. --%>
String name1 =  request.getParameter("name");
<%-- name의 파라미터 변수가 가진 값을 전달받아 name1에 저장한다. --%>

include 액션태그 2
<Br>
include 액션태그를 사용하여 include_3.jsp에 입력받은 이름을 전달합니다.
<hr width = 300 align=left></hr>

<jsp:include page="include_3.jsp">
 <jsp:param name=name2 value="<%=name1%>" />
</jsp:include>

<%-- 포함되는 페이지인 include_3.jsp에 파라미터 변수 name2 전달함.--%>

<br>

</BODY>
</HTML>

<%-- include_1.jsp 에서 넘겨 받은 name 데이터를 form 태그를 통해서 include_2.jsp 로 넘겨준다. 하지만 include_2.jsp 에는 request.getParameter()가 없다. 없는데 어떻게 출력이 되는 것인가.? 어떻게 데이터를 넘겨 받았나? include를 이용한 것이다.
include_2.jsp 에서 include_3.jsp 에 있는 페이지를 포함시키고 include_3.jsp 에 있는 request.getParameter()를 이용해서 include_2.jsp에서 name3에다가  데이터를 넘겨받는다. include_3.jsp 페이지에서  include_1.jsp 에서 입력한 name을 출력을 한다. 육안으로는 include_2.jsp 에서 출력되는 것 처럼 보이지만 코드상으로는  include_3.jsp 에서 출력하는 것이다. include_2.jsp에 include_3.jsp 페이지를 포함시키기 때문에 그렇게 보이는 것이다. %-->



<%-- include_3.jsp --%>

<%@page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
   
<HTML>
<HEAD><TITLE>include_3.jsp</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
</HEAD>

<BODY>
include 액션태그 3
<br>
<%
String name3 =  request.getParameter("name2");
%>

<%-- include_2.jsp 에서 전달받은 파라미터 값(name2)을 name3 저장한다.--%>

<%=name3%>님 반갑습니다.
<hr width=300 align=left></hr>

</BODY>
</HTML>

728x90

'programming > jsp' 카테고리의 다른 글

자바 빈즈 수업 메모  (0) 2010.04.30
JSP - include 액션 태그  (0) 2010.04.09
JSP 실습 5 (연산)  (0) 2010.04.09
JSP 실습 4(form 태그 )  (0) 2010.04.09
포워드(forward)와 리다이렉트(redirect) 차이  (0) 2010.04.07