본문 바로가기

JSP/기본다지기

JSP 3일차 필기 (jsp 데이터타입, 임포트)

새로운 프로젝트 L04Jsp 생성


WebContent에 index.jsp 생성


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=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp에 대해서 알아보자</title>
</head>
<body>
    <h1>jsp 사용법</h1>
    <hr>
    <h3><a href="./L01DataType.jsp">[jsp의 데이터 타입]</a></h3>
    <h3><a href="./L02Import.jsp">[jsp에서 다른 클래스 import 하기]</a></h3>
    <h3><a href="./L03GolbalV.jsp">[jsp에서 전역변수 설정하기]</a></h3>
    <h3><a href="./L04Method.jsp">[jsp에서 함수 사용하기]</a></h3>
    <h3><a href="./L05If.jsp">[jsp에서 if문 사용하기]</a></h3>
</body>
</html>
cs


먼저 데이터 타입을 알아보기 위한 예제인 L01DataType.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
36
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>기본 데이터 타입을 알아보자</title>
</head>
<body>
    <h1>기본 데이터 타입</h1>
<%
char a = 'a';
String b = "Hello jsp";
int c = 10;
long l = 222222222222222222L;
float f = 2.5F;
double d = 255.22;
%>
<% out.print("<h3> char a : "+a+"</h3>"); %>
<h3>String b : <%=b%></h3>
<!-- %= 은 out.print();의 괄호 내부 -->
<h3>int c : <%=c%></h3>
<h3>long l : <%=l%></h3>
<h3>float f : <%=f%></h3>
<h3>double d : <%=d%></h3>
<h3>++c : <%=++c%></h3>
<h3>--c : <%=--c%></h3>
<h3>c/3 : <%=c/3%></h3>
<h3>c%3 : <%=c%3%></h3>
<h3>l+c : <%=l+c%></h3>
<h3>d/c : <%=d/c%></h3>
<h3>d/f : <%=d/f%></h3>
<h3>d-0.22 : <%=d-0.22%></h3>
<h3>d*3 : <%=d*3%></h3>
</body>
</html>
cs


자바와 거의 동일한 방식으로 데이터 타입을 사용할 수 있으나,


자바 코드를 사용하기 위해서는 스크립트릿을 사용해야한다.




임포트를 알아보기 위한 L02Import.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 import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.HashMap, java.util.Set, java.util.Map, java.util.Iterator"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>util 패키지의 HashMap 임포트하기</title>
</head>
<body>
<%
    HashMap<String,Object> map = new HashMap<String,Object>();
    map.put("one"1);
    map.put("two"2.0);
    map.put("three""three");
    map.put("four"'4');
    //Set, Iterator로 map의 모든 자원을 출력하세요. or ForEach로 출력
    Set<Map.Entry<String, Object>> entries = map.entrySet();
    Iterator<Map.Entry<String, Object>> it = entries.iterator();
    while(it.hasNext()){
        Map.Entry<String, Object> entry = it.next();
%>
    <p><b><%=entry.getKey()%>:</b><%=entry.getValue()%></p>
<%
    }
    out.print("<hr>");
    for(Map.Entry<String,Object> entry : entries){
%>
    <p><b><%=entry.getKey()%>:</b><%=entry.getValue()%></p>
<%
    }
%>
</body>
</html>
cs


위 예제와 같이 스크립트릿을 이용하여 임포트 할 수 있다.


Set_Iterator와_ForEach_복습