JSP 주석
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%
// 2번째 줄 : 불필요한 공백 제거
// 스크립릿(java 코드 영역) - java 주석(자바코드 영역에서만 가능)
int s = 0;
for(int i = 1; i<=100; i++) {
s += i;
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3> JSP 주석 </h3>
<!-- HTML 주석 : 클라이언트에게 전송됨 (네트워크 사용) -->
<%-- JSP 주석 : 클라이언트에게 전송되지 않음 --%>
<p>
1에서 100까지 합은 <%=s%> 입니다.
</p>
</body>
</html>
표현식
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>표현식</h3>
<p>
자바 코드의 결과를 클라이언트에게 전송하기 위해 사용한다.<br>
표현식은 out.print(코드); 형식의 자바 코드로 변환 된다.<br>
표현식은 서버측에서 실행되고 실행결과(문자열)을 클라이언트에게 전송한다.
</p>
<hr>
<p>
합 : <%= 1+2+3+4+5 %>
</p>
<p>
<%
// 스크립 릿
int n = 10;
String result = n%2 == 0 ? "짝수" : "홀수";
%>
<%= n %>은 <%=result %> 입니다.
</p>
</body>
</html>
JSP 지시어(directive)
page 지시어 - JSP 페이지의 설정 정보 지정
<!-- page의 contentType 속성 : 클라이언트에게 전송하는 문서의 타입 설정. 생략하면 인코딩은 ISO-8859_1로 설정 -->
<%@ page contentType="text/html; charset=UTF-8"%>
<!-- page의 import 속성 : 필요한 자바 클래스 import -->
<%@ page import="java.util.Calendar"%>
<!-- 불필요한 공백 제거 -->
<%@ page trimDirectiveWhitespaces="true"%>
<%
Calendar cal = Calendar.getInstance();
String s = String.format("%tF %tT", cal, cal);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>지시어(directive)</h3>
<p>
page 지시어 : JSP 페이지의 설정 정보 지정<br>
</p>
<p>
<%=s %>
</p>
</body>
</html>
page 지시어 buffer : 출력할 버퍼의 크기 지정 / autoFlish : 버퍼가 찬 경우 버퍼를 비울지의 여부를 지정
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page buffer="1kb" autoFlush="true" %>
<%@ page trimDirectiveWhitespaces="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>page 지시어(directive)</h3>
<p>
page 지시어 : JSP 페이지의 설정 정보 지정<br>
buffer : 출력할 버퍼의 크기 지정<br>
autoFlush : 버퍼가 찬 경우 버퍼를 비울지의 여부를 지정(기본:true-버퍼를 비움)
</p>
<!--
buffer의 크기가 1kb이고 autoFlush="false" 환경에서 출력 내용이 4kb가 넘으므로 오류가 발생함.
autoFlush="true"로 변경하면 에러가 없어짐
-->
<% for(int i=1; i<=1000; i++) { %>
1234
<% } %>
</body>
</html>
page 지시어 errorPage 속성 : 에러가 발생할 경우 보여줄 페이지 지정
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page errorPage="error.jsp" %>
<%@ page trimDirectiveWhitespaces="true"%>
<%
String s = "a";
int n = Integer.parseInt(s);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>page 지시어(directive)</h3>
<p> errorPage 속성 : 에러가 발생할 경우 보여줄 페이지 지정</p>
<hr>
<p><%= n %></p>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page isErrorPage="true" %> <!-- 에러가 발생할 때 보여지는 페이지임을 설정 -->
<%@ page trimDirectiveWhitespaces="true"%>
<%
// 에러 페이지가 아닌 정상적인 페이지임을 설정
response.setStatus(HttpServletResponse.SC_OK);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>정보</h3>
<p>
에러가 발생했습니다. 잠시 후 다시 실행 하세요.
</p>
</body>
</html>
include 지시어
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>include지시어</h3>
<p>
JSP 페이지에 다른 페이지의 내용을 포함할 때 사용한다.<br>
JSP 파일을 JAVA로 변환할 때 처리하며, 복사&붙여넣기 개념과 유사하다.
</p>
<hr>
<%@ include file="ex07_inc.jsp" %>
<p>
<%=name %> 홈페이지 입니다.
</p>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%
String name = "이자바";
%>
<h3>환영합니다.</h3>
스크립릿, 표현식 예
(1) 방법
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%
int row = 10;
int col = 7;
int n = 0;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>스크립릿, 표현식 예</h3>
<table border="1" style="margin: 30px auto; width: <%=col*50%>px; border-collapse: collapse;">
<% for(int i=1; i<=row; i++) { %>
<tr height="30px" align="center">
<% for(int j=1; j<=col; j++) { %>
<td><%= ++n %></td>
<% } %>
</tr>
<% } %>
</table>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%
int row = 10;
int col = 7;
int n = 0;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>스트립릿, 표현식 예</h3>
<p>
out 객체 : JSP 내장 객체로 클라이언트에게 정보(문자열)를 전송하는 객체이다.
</p>
<hr>
<table border="1" style="margin: 30px auto; width: <%=col*50%>px; border-collapse: collapse;">
<%
for(int i=1; i<=row; i++) {
out.print("<tr height='30' align='center'>");
for(int j=1; j <=col; j++) {
out.print("<td>" +(++n)+"</td>");
}
out.print("</tr>");
}
%>
</table>
</body>
</html>
구구단 짜보기
더보기
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<style type="text/css">
p {
margin: 0;
}
</style>
</head>
<body>
<h3>표현식, 스크립릿</h3>
<hr>
<h4>구구단 - 방법1</h4>
<% for(int dan=2; dan<=9; dan++) { %>
<p>** <%=dan %>단 **</p>
<%for(int n=1; n<=9; n++) { %>
<p><%= dan %> * <%= n %> = <%= dan*n %></p>
<% } %>
<p>----------------</p>
<% } %>
<h4>구구단 - 방법2</h4>
<%
for(int dan=2; dan<= 9; dan++) {
out.print("<p>** " + dan + "단 **</p>");
for(int n=1; n <=9; n++) {
out.print("<p>" + dan +" * " + n +" = "+ (dan*n) +"</p>");
}
out.print("<p>-------------</p>");
}
%>
</body>
</html>
'쌍용강북교육센터 > 10월' 카테고리의 다른 글
1008_Servlet/JSP : 파라미터 전송 GET 방식 / POST 방식 / 서버에 데이터 보내기 전 유효성 검사 (0) | 2021.10.10 |
---|---|
1008_Servlet/JSP : 선택된 체크박스의 값 반환하기 // 복습 (0) | 2021.10.10 |
1008_Servlet/JSP : (CH01) 예제 (0) | 2021.10.10 |
1008_Servlet/JSP : Servlet 이란 ? (0) | 2021.10.10 |
1008_Servlet/JSP : 웹 어플리케이션 / HTTP (0) | 2021.10.10 |