// JSP 구성 요소
- 스크립트 요소
- JSP 지시자
- 액션 태그
// 스크립트 요소, Scripting Elements
a. 스크립틀릿
~ <% %>
- 이 영역은 자바 영역임을 선언
- 순수 자바 코드를 HTML 페이지에서 작성할 수 있도록 선언
- Scriptlet = Script + Applet
- 대부분의 비즈니스 코드 작성 담당(대부분의 업무 + 알고리즘 구현)
- 가장 중요한 역할
b. 익스프레션
~ <%= 값 %>
- 자바의 값을 HTML 문서에 츨력한다.
- 값이 아무것도 들어있지 않으면 에러가 발생한다.
c. 선언부
~ <%! %>
- JSP 페이지 내에서 사용되는 변수 또는 메서드를 선언할 때 사용한다.
- 선언부 내에서 선언된 변수 및 메서드는 전역 멤버이다.
<%@page import="com.test.jsp.Util"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = "홍길동";
int age = 20;
String color = "orange";
String txt = "<input type='text'>";
String attr = "background-color";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
div {
color: <%= color %>;
<%= attr %>: beige;
}
</style>
</head>
<body>
<%
//자바 영역
int a = 10;
int b = 20;
int c = a + b;
String str = name;
%>
<div><%= a %> + <%= b %> = <%= c %></div>
<div><%= name %></div>
<input type="text" value="<%= age %>">
<hr>
<%= txt %>
<input type="button" value="확인" id="btn1" style="color:<%= color %>">
<hr>
결과: <%-- <%= add(100,200) %> --%>
결과: <%= Util.add(100,200) %>
<script>
document.getElementById('btn1').onclick = function() {
alert('<%= name %>');
};
</script>
</body>
</html>
<%@ 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 res = 0;
private int sqr(int a) {
return a * a;
}
%>
<%
for(int i = 1; i <= 5; i++) {
res += sqr(i);
}
%>
result = <%= res %>
</body>
</html>
'서버 > Servlet-JSP' 카테고리의 다른 글
JSP 구성 요소 - 액션 태그 (0) | 2023.01.03 |
---|---|
JSP 구성 요소 - JSP 지시자 (0) | 2023.01.01 |
Servlet Parameter (0) | 2022.12.30 |
Servlet 생명주기 (0) | 2022.12.29 |
Servlet-JSP GET 방식, POST 방식 (0) | 2022.12.25 |