티스토리 뷰

Ctrl + Shift + M : import

import는 선언부 밑에 한다.

1. list 반복하기

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>   
 
 <%
 
     List<String> news = new ArrayList<String>();
     news.add("MBC 납량특집 드라마 M");
     news.add("MBC 납량특집 드라마 거미");
     news.add("KBS 납량특집 전설의 고향");
     news.add("SBS 토요 미스테리");
     
 
 %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <table border="1">
        <tr>
            <th>무서운거......</th>
        </tr>
<%
    for ( String drama : news ) {
%>
        <tr>
            <td><%= drama %></td>
        </tr>
<% } %>
    </table>
</body>
</html>
cs

 

jsp 는 두가지 스크립트릿이 있다.

1. <%      %>
2. <%!     %>    클래스를 만드는 선언부 (이 방법 쓰는 대신 자바 파일을 만들어서 쓰는 것이 편하다)
   cf. <%@    %> 디렉토리를 만드는 선언부 

2번 방법을 활용하면 굳이 class를 만들지 않아도 쓸 수 있다. 지금처럼 만들면! 그런데 여러가지 데이터가 있을 때 클래스를 만드는 것은 필수이다. 그래서 이것을 만드는 방법을 알아보자.

1. src에서 VO class를 만든다.

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
package com.ktds.smahn;
 
public class DramaVO {
    
    private String dramaName;
    private String broadcastStation;
    
    //Getters and Setters
    public void setDramaName(String dramaName) {
        this.dramaName = dramaName;
    }
    
    public void setBroadcastStation(String broadcastStation) {
        this.broadcastStation = broadcastStation;
    }
    
    public String getDramaName() {
        return this.dramaName;
    }
    
    public String getBroadcastStation() {
        return this.broadcastStation;
    }
    
    
}
 
cs

2. jsp파일에 import하고 반복문을 활용해서 출력한다.

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
<%@page import="com.ktds.smahn.DramaVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>  
 
 <%
 
     DramaVO vo = null;
     
     List<DramaVO> dramas = new ArrayList<DramaVO>();
     for ( int i = 0; i < 4; i++ ) { 
         vo = new DramaVO();
         vo.setBroadcastStation("MBC");
         vo.setDramaName("M" + i);
         dramas.add(vo);
     }
 
 %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <table border="1">
        <tr>
            <th>방송사</th>
            <th>드라마 제목</th>
            
        </tr>
<%
    for ( DramaVO drama : dramas ) {
%>
        <tr>
            <td><%= drama.getBroadcastStation() %></td>
            <td><%= drama.getDramaName() %></td>
        </tr>
<% } %>
    </table>
</body>
</html>
cs

 

여기에 데이터베이스가 추가되면, 그곳에서 하나하나 꺼내서 출력해주면 된다. 

body밖에 스크립트릿를 쓴 것은, body 내에서 스크립트릿을 쓰기 위해서 써야하는 것들을 미리 밖에서 써주는 것이 일반적이기 때문이다.

javascript에서 jsp쓰는 것은 가능하나, jsp에서 javascript쓰는 것은 불가능하다.

 


 

정리!

1. 테이블 만들기

2. 테이블과 버튼을 이용한 계산기 만들기. 동작을 하기 위해 자바스크립트를 활용.
   document.getElementById() 가 중요. 어떤 태그를 자바스크립트를 가져와서 동적으로 바꾸어주기 위함. 다른 페이지로 결과값 전송.

3. 자바와 jsp를 모두 활용하여 리스트를 만들어서 반복을 시키는 것. 하나의 자바로직을 만들기 위해서 스크립트릿이 반드시 이어져야할 의무는 없다.

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함