chap03 JSP 기본 구조
1. JSP-HTML 기본 코드 구조
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>test</title>
</head>
<body>
<%
// Java code
String name = "test";
String email = "test@test.com";
%>
<h1>
Name: <%=name%>, Email: <%=email%>
</h1>
</body>
</html>
2. JSP 구성요소
2.1 디렉티브(Directive)
- JSP 설정 정보 지정
- <%@ 디렉티브 설정1="값1" 설정2="값2" ...%>
- <%@ page contentType="text/html; charset=UTF-8" %>
- 디렉티브 종류 : page / taglib / include
2.2 JSP 스크립트 요소
- 스크립트릿(Scriptlet) : 자바 코드
- 표현식(Expression) : 출력
- 선언부(Declaration) : 변수, 함수등 정의
- 표현 언어(EL : Expression Language)
- JSTL (JSTL(JSP Standard Tag Library))
2.3 JSP 기본 객체
- 클래스 기본 생성 됨
- out
- request, page, session, application, page 등
- response
2.4 JSP 기본
2.4.1 스크립트릿(Scriptlet)
- <% java code 입력 %>
2.4.2 표현식(Expression)
- <%=변수 %>
- <% out.println("값")%>
2.4.3 변수 선언
var.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>
<h1>
변수간 값 바꾸기 <br>
<%
int a = 10;
int b = 20;
int temp = 0;
temp = a;
a = b;
b = temp;
%>
Result : a = <%=a %>, b = <%=b %>
</h1>
</body>
</html>
2.4.4 연산자
operator.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>
<h1>연산자</h1>
<h2>산술연산</h2>
<%
int a = 10;
int b = 20;
%>
a + b = <%=a+b %><br>
a - b = <%=a-b %><br>
a * b = <%=a*b %><br>
a / b = <%=a/b %><br>
a % b = <%=a%b %><br>
<h2>비교연산</h2>
a == b = <%=a==b %><br>
a >= b = <%=a>=b %><br>
a > b = <%=a>b %><br>
a <= b = <%=a<=b %><br>
a < b = <%=a<b %><br>
a != b = <%=a!=b %><br>
<h2>논리연산</h2>
a == b && a <= b = <%=a == b && a <= b %><br>
a == b || a <= b = <%=a == b || a <= b %><br>
!(a == b) = <%=!(a == b) %><br>
</body>
</html>
2.4.5 조건문
if.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>
<h1>조건문 if</h1>
<%
String role = "admin";
if(role == "admin"){
out.println("ADMIN <br>");
}else{
out.println("GUEST <br>");
}
%>
<hr>
<%
role = "guest";
if(role == "admin"){
%>
ADMIN <br>
<%
}else{
%>
GUEST <br>
<%
}
%>
</body>
</html>
2.4.6 반복문
oneToTen.jsp
<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<head><title>1-10까지의 합</title></head>
<body>
<%
int sum = 0;
for (int i = 1 ; i <= 10 ; i++) {
sum = sum + i;
}
%>
1 부터 10까지의 합은 <%= sum %> 입니다.
</body>
</html>
2개 스크립트릿 사용 sum.jsp
<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<head><title>합 구하기</title></head>
<body>
<%
int sum = 0;
for (int i = 1 ; i <= 10 ; i++) {
sum = sum + i;
}
%>
1 부터 10까지의 합은 <%= sum %> 입니다.
<br>
<%
int sum2 = 0;
for (int i = 11 ; i <= 20 ; i++) {
sum2 = sum2+ i;
}
%>
11 부터 20까지의 합은 <%= sum2 %> 입니다.
</body>
</html>
for.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>
<h1>Heading Tag 1<h1>
<h2>Heading Tag 2<h2>
<h3>Heading Tag 3<h3>
<h4>Heading Tag 4<h4>
<h5>Heading Tag 5<h5>
<h6>Heading Tag 6<h6>
<hr>
<h1>반복문 for</h1>
<%
for(int i=1; i<=6; i++){
%>
<h<%=i %>>Heading Tag <%=i %><h<%=i %>>
<%
}
%>
<h1>반복문 while</h1>
<%
int i = 1;
while(i<=6){
%>
<h<%=i %>>Heading Tag <%=i %><h<%=i %>>
<%
i++;
}
%>
</body>
</html>
2.4.7 메서드 사용
useDecl2.jsp
<%@ page contentType = "text/html; charset=utf-8" %>
<%!
public int add(int a, int b) {
int c = a + b;
return c;
}
public int subtract(int a, int b) {
int c = a - b;
return c;
}
%>
<html>
<head><title>스크립트릿에서 선언부 사용하기</title></head>
<body>
<%
int value1 = 3;
int value2 = 9;
int addResult = add(value1, value2);
int subtractResult = subtract(value1, value2);
%>
<%= value1 %> + <%= value2 %> = <%= addResult %>
<br>
<%= value1 %> - <%= value2 %> = <%= subtractResult %>
</body>
</html>
2.4.8 클래스 사용
time.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>
<h1>
<%
Date date = new Date();
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String strdate = simpleDate.format(date);
%>
Today : <%=strdate%>
</h1>
</body>
</html>