[JSP] 10. include객체

include 내장객체를 이용한 값의 공유/ 페이지 이동

10) Include 공유를 이용하는 문제입니다.


include1.jsp을 실행하면, include2.jsp도 실행되도록 include를 사용합시다.

request를 이용해서 include(jsp액션)=> getParameter()

                        => gerAttribute()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.Date, java.text.SimpleDateFormat"%>
   
<!--  Date와 SimpleDateFormat를 import한다 --> 
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8">
<title> 공통적으로 사용 </title>
</head>
<body>
<%= "오늘의 날씨정보는 맑음입니다" %>
<%
    //Date now= new Date();
    //SimpleDateFormat sdf = new SimpleDateFormat("HH : mm : ss");
    
    //변수명없이
    //String result = new SimpleDateFormat("HH : mm : ss").
    //format(new Date());
%>
<%="지금은"+new SimpleDateFormat("HH : mm : ss").format(new Date())+"입니다" %>
<%-- <%=share%> --%>
 
 
<!-- 현재호출파일명 리턴 -->
<%=request.getRequestURI() %>
<!-- request.getParameter() 메서드--> 
<!-- request.getRemoteAddr() 메서드--> 
 
</body>
</html>




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8">
<title> jsp의 include태그 </title>
</head>
<body>
<%-- <% int share=100; %> --%>
<h3><%@include file="common.jsp" %></h3>
<h1>메인 메뉴입니다.</h1>
<%@include file="common.jsp" %> 

<!-- 변수명이 같을경우는 오류가 뜨기 때문에, 
common.jsp에서 변수명을 지정하지않는 방식으로 코드를 작성하였더니 코드에 오류가 없다. -->
<h3>서브 메뉴입니다.</h3>                

<!-- include를 통해 변수를 공유할 수 있고, 
jsp=>서블릿(자바)파일에서는 심플데이터포맷이 호출할때마다 작성되있는걸 볼 수 있다. -->
<%@include file="common.jsp" %>
</body>
</html>





본격적인 include에 대해서 알아보도록 합시다.


include1.jsp 파일이 있습니다.

include2.jsp 파일이 있습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8">
<title> include1을 실행하면, include2도 실행되도록 :clude를 사용해라 </title>
<!-- 이번에 공유하는 방법은 각각의 소스가 따로있고, 실행결과만 합치는 것. -->
</head>
<body>    
    <%--<jsp:include page="include2.jsp"/> 여러번 사용가능한 것 확인후 주석 --%>
    <% String id = request.getParameter("id");
       String check= "id정상입력"; //include2.jsp출력되도록 전달하기
       request.setAttribute("condition2", check); 
//한글인코딩을 방지하기위하여, 자바의 객체를 param으로 전달할 수 없기때문에 setAttribute를 사용해야한다.
    %>
    <%--<jsp:include page="include2.jsp"/>--%>         
    <h1>입력아이디 <%=id %> 회원님 로그인하셧습니다.</h1>
                                                
<!-- 사용자가 정의한 내용들 외에 추가로 더 넣고 싶을때, 
(객체가 아닌 것들중에서) html에서는 객체를 전송하지 못하기때문에, 그럴때 그외에 추가적으로 넣을것들. -->
     <jsp:include page="include2.jsp">        
         <jsp:param value="check" name="condition"/> 
         <jsp:param value="id정상입력" name="condition"/
          
     
     </jsp:include<!-- (1) jsp:clude는 /슬래시로 끝난다.
                         (2) 바디가 있을경우 /를 적지 않는다. 
                         (★) jsp:안에서는 주석을 사용할 수 없다.-->
     <%-- 정보를 전송한다 --%>
</body>
</html>




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*, java.text.*;"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% Date now = new Date();
       SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
       String logintime = sdf.format(now);
    %>
   <h3>로그인 시각: <%=logintime %></h3>
<!-- 포함되는 파일에서는 포함하는 파일로 정보 전달x -->
<!-- 포함되는 파일에서는 포함하는 파일로부터 정보전달o -->
<!-- 1.요청정보 사용자 입력 변수 -->
  
    <%= request.getParameter("id") %><br> 
<!--  request자체가 공유되기때문에 그대로 사용한다. request와 response를 동시에 사용 -->
   
<!-- 2. jsp파일에서 전달 변수1하고, include1.jsp의 전달태그가 param태그이기때문에 include2.jsp에서 getParameter로 출력한다 -->
   <%= request.getParameter("condition") %><br>
   
<!-- 3. jsp파일에서 전달 변수2하면서, setAttribute를 사용했기때문에 무조건 getAttribute가 와야한다. -->
   <%= request.getAttribute("condition2") %><br>  
   <%--받은 정보를 출력한다. --%>
</body>
</html>