본문 바로가기

DATA 분석 교육 과정 (2024.02~08)/JSP&Servlet

JSP_JSP의 필요성 , 기본문법(<%,<%=,<%!)

728x90
Servlet, JSP 비교

 

 

JSP 기본문법

 

<%@ 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>yyj</h1>

<!-- 1. 스크립트릿 -->
<%
// java코드 작성하는 부분
	int a = 1;
	int b = 2;
    System.out.println(a+b);// consol 창에 나옴
%>
<!-- 2. 표현식: web에 동적인 데이터를 출력하기 위한 표현식 문법-->

<h2> a+b의 결과: <%=a+b+4 %></h2> <!-- // web창에 나옴 -->


<!-- 3. 선언문: static 변수, 메소드 등 클래서 영역에 선언하기 위해 사용하는 문법 -->

<h1>선언문</h1>
	<%!  //합을 구하는 메소드
	public int addNum(int num1, int num2) {
		return num1 + num2;
	}%>

	<%
	System.out.println(addNum(1, 2));
	%>
	<%=addNum(1, 2)%>

</body>
</html>

출처: 스마트인재개발원

728x90
반응형