[JSP] JSP 태그 실습-1

- 다음과 같은 입출력의 형태를 생성 할 것
- HTML, JSP 파일을 나누어 생성

※ 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
<%@ 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>
    
    <%
    int row =  Integer.parseInt(request.getParameter("row"));
    int col =  Integer.parseInt(request.getParameter("col"));
    %>
    
    <h1><%=row %></h1><br>
    <h1><%=col %></h1>
 
    <%-- 내장 객체를 이용하여 생성 --%>
    <table border="1px">
    <%
        for(int i =0; i<row; i++){
            out.write("<tr>");
            for(int j = 1; j<=col; j++){
                out.write("<td>"+(i*col+j)+"</td>"); //1부터 j*i의 수 출력(테이블 내부에서 )
            }
            out.write("</tr>");
    }%>
    </table>
    
    
</body>
</html>
cs

!) 내장 객체가 아닌 단순 태그 만을 이용한 방법
1
2
3
4
5
6
7
8
9
10
    <table border="1px">
    <%
        for(int i =0; i<row; i++){%>
            <tr>
            <%for(int j = 1; j<=col; j++){%>
                <td><%=i*col+%></td>
            <%}%>
            </tr>
    <%}%>
    </table>
cs
- 중괄호를 jsp 태그로 각각 닫아 별도로 처리


※html 파일 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>생성할 행, 열 수를 입력해주세요.</h1>
    <form action="ex09makeTable.jsp">
        행 : <input type="number" name = "row"><br>
        열 : <input type="number" name = "col"><br>
        
        <input type="submit">
    </form>
</body>
</html>
cs


댓글