본문 바로가기
IT/JSP

[JSP]include를 이용한 액션 테스트

by dya0 2019. 5. 14.

위와 같은 결과를 만들기 위해서 아래와 같이 파일을 만든다.

 

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
36
37
38
39
40
41
42
43
44
45
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>포워드 액션 테스트</title>
<style>
body{
margin:0 auto;
text-align:center;}
form {
    background: green;
    height: 100%;
    color: white;
    text-align: center;
}
table{text-align:center;}
</style>
</head>
<body>
    <form action="IncludeTest2.jsp" method="post">
    <!-- hidden 타입의 input으로 다음 페이지에서 사용할 주소값 지정 -->
    <!-- 가져가야할 값은 가져감 -->
    <input type = "hidden" name ="includePage" value = "IncludeTest3.jsp">
        <table>
            <tr>
                <td>이름</td>
                <td><input type="text" name="name" required></td>
            </tr>
            <tr>
                <td>나이</td>
                <td><input type="text" name="age" required></td>
            </tr>
            <tr>
                <td>주소</td>
                <td><input type="text" name="address" required></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="전송"></td>
            </tr>
        </table>
    </form>
</body>
</html>
Colored by Color Scripter

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>IncludeTest</title>
</head>
<body>
<h2>인클루드 액션 테스트 시작</h2>
<%request.setCharacterEncoding("euc-kr"); %>
<jsp:include page = '<%=request.getParameter("includePage") %>'>
    <jsp:param name = "tel" value="010-1234-5678"/>
    <jsp:param name ="alias" value = "happy"/>
    </jsp:include>
    <h2>인클루드 액션 테스트 end</h2>
</body>
</html>
Colored by Color Scripter

앞서 사용한 forward와 달리 include는 위와 아래에 있는 h2 태그의 내용이 출력된다.

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 import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<style>form{margin:0 auto;}
table {text-align:center;}
tr>td:first-child{background-color:orange;text-align:center;width:200px;height:50px;}
tr>td:last-child{background-color:yellow;text-align:center;width:350px;}
</style>
<meta charset="EUC-KR">
<title>인클루드</title>
</head>
<body>
    <form>
    <table>
    <%
    Enumeration<String> e = request.getParameterNames();
    while(e.hasMoreElements()){
        String attributeName = e.nextElement();
        String attributeValue = request.getParameter(attributeName);
    %>
    <tr>
    <td><%=attributeName %></td>
    <td><%=attributeValue %></td>
    </tr>
    <%%>
        </table>
    </form>
</body>
</html>
Colored by Color Scripter

그 이유는 아래의 그림을 보면 알 수 있다.

includeTest2에서 파일을 실행하고 다 만든 후에 includeTest로 가지고 와서 삽입하기 때문이다.

'IT > JSP' 카테고리의 다른 글

[JSP]자바빈 JAVABEAN  (0) 2019.05.14
[JSP]include로 화면 구현하기  (0) 2019.05.14
Forward Action 연습 1  (0) 2019.05.14
액션 태그  (0) 2019.05.14
Error page 관리  (0) 2019.05.13