너와 나의 연결고리, JSP 🔗(5) - JSP, 내장객체, 스크립트태그, 디렉티브태그, 액션태그

joyfulwave·2022년 10월 12일
0

이제 진짜 웹 서버와 연결해 볼 차례




📚 JSP

  • JSP = JavaServer Pages
  • HTML 코드에 JAVA 코드를 넣어 동적 웹페이지를 생성하는 웹어플리케이션 도구
  • jsp가 실행되면 자바 서블릿으로 변환되며, 웹 어플리케이션 서버에서 동작되면서 필요한 기능을 수행하고 그렇게 생성된 데이터를 웹페이지와 함께 클라이언트로 응답해요.
  • JSP는 서블릿 기반의 '서버 스크립트 기술'이에요.
  • JSP : html + java(HTML을 중심으로 JAVA와 연동하여 사용하는 웹 언어)



📚 스크립트 태그

HTML 태그에 자바 코드를 넣어 프로그램이 수행할 기능을 구현할 수 있어요.

⚫ 선언문(declaration) : <%! %>

  • 자바 변수나 메소드를 정의하는데 사용하는 태그

⚫ 스크립틀릿(scriptlet) : <% %>

  • 자바 변수 선언 및 자바 로직 코드를 작성하는데 사용되는 태그

⚫ 표현문(expression) : <%= %>

  • 변수, 계산식, 메소드 호출 결과를 나타내는 태그

⚫ 지시자 : <%@ %>

  • 페이지 속성 지정

⚫ 주석 : <%-- --%>

  • 주석처리

📌 예제

<%@ 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>
	<table border="1">
		<tr>
			<%for(int i = 1; i <= 5; i++) {%>
				<td>1행 <%=i %> 열</td>
			<%} %>
		</tr>	
	</table>
</body>
</html>




📚 JSP 내장 객체

📌 내장객체

  • jsp페이지 내에서 제공되는 특수한 클래스 형의 객체
  • 내장 객체는 선언과 할당 없이 사용할 수 있어요.

📌 내장객체 종류

  • request : 웹 브라우저의 요청 정보를 저장하고 있는 객체

  • response : 웹 브라우저의 요청에 대한 응답 정보를 저장하고 있는 객체

  • out : JSP 페이지에 출력할 내용을 가지고 있는 출력 스트림 객체

  • session : 하나의 웹 브라우저의 정보를 유지하기 위한 세션 정보를 저장하고 있는 객체

  • application

  • pageContext

  • page

  • config

  • exception




📚 디렉티브 태그

  • 현재 JSP페이지의 특정 영역에 외부 파일의 내용을 포함시키는 태그에요.
<%@ include file="파일명" %>

📌 예제

⚫ include_test.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>
	<%@ include file="header.jsp" %>
	<h4>--------------------- 현재 페이지 영역 ---------------------</h4>
	<p>페이지 내용들 !!!</p>
	<h4>---------------------------------------------------------</h4>
	<%@ include file="footer.jsp" %>
</body>
</html>

⚫ header.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>
	<p>여기는 헤더 영역</p>
</body>
</html>

⚫ footer.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>
	Copyright KoreaIT
</body>
</html>

⚫ 결과




📚 액션태그

  • 액션태그는 행위에 대한 태그에요.
  • JSP 페이지에서 동일한 내용이라도 자바 코드를 기술하기 보다는 태그를 기술하는 것이 지저분하지 않고 깔끔하게 코딩할 수 있어 가독성 높은 소스코드를 작성할 수 있어요.
  • 코드양을 대폭 줄일 수 있다.

📌 액션태그 종류

⚫ forward

: 다른 페이지로 이동

<jsp:forward />

⚫ include

: 외부 페이지의 내용을 포함하거나 페이지 모듈화

<jsp:include />

⚫ param

: 현재 페이지에서 다른 페이지에 정보를 전달할 때 사용

<jsp:param />

⚫ useBean

: 빈(Bean, 객체)을 생성하고 사용하기 위한 환경을 정의

<jsp:useBean />

⚫ setProperty

: 빈에서 속성 값을 할당

<jsp:setProperty />

⚫ getProperty

: 빈에서 속성 값을 얻어올 때 사용

<jsp:getProperty />

📌 forward 사용 예제

⚫ forward_test.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>
	<form action="controller.jsp">
		<select name="site">
			<option value="naver">네이버</option>
			<option value="google">구글</option>
			<option value="daum">다음</option>			
		</select>
		<input type="submit">	
	</form>
</body>
</html>

⚫ controller.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%
	String site = request.getParameter("site");

	String requestURL = "";
	if(site.equals("naver")){
		requestURL = "forward_naver.jsp";
	}else if(site.equals("google")){
		requestURL = "forward_google.jsp";
	}else if(site.equals("daum")){
		requestURL = "forward_daum.jsp";
	}
%>

	<jsp:forward page="<%=requestURL %>"/>
	
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

⚫ forward_naver.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>
	<script>
		window.open("https://www.naver.com", "_self");
	</script>
</body>
</html>

⚫ forward_google.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>
	<script>
		window.open("https://www.google.com", "_self");
	</script>
</body>
</html>

⚫ forward_daum.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>
	<script>
		window.open("https://www.daum.net", "_self");
	</script>
</body>
</html>



포기하지 말고 JUST DO! ✔️




출처
https://media.giphy.com/media/dwmNhd5H7YAz6/giphy.gif
https://media.giphy.com/media/3o6Mb9EC7mNqXl9x7y/giphy.gif

0개의 댓글