너굴 개발 일지

TIL_210422_JSP 표준 액션 태그 본문

JSP,Serlvet

TIL_210422_JSP 표준 액션 태그

너굴냥 2021. 4. 25. 01:51

 

목차

- 표준 액션 태그

- <jsp:forward>
- <jsp:include>
- <jsp:param>

 

 

 

 

표준 액션 태그

미리 정해진 기능들을 JSP 스펙에 명시함으로써 모든 컨테이너가 동일하게 구현하는 태그

모든 JSP 컨테이너에서 기본으로 제공하고 있어서 기본 액션 태그라고도 함

태그 사용시에는 <태그 라이브러리 이름 : 태그 이름> 으로 함

 

표준 액션 태그 종류 

jsp : attribute

jsp : forward

jsp : getProperty

jsp:include

jsp:param

jsp:setProperty

jsp:useBean

나머지는 생략

 

 

 

<jsp:forward>

forward 표준 액션 태그는 RequestDispacther 객체의 forward() 메소드 기능을 실행

하나의 jsp 페이지에서 다른 jsp 페이지로 요청 처리를 전달할 때 사용

현재 실행중인 페이지와 forward에 의해 호출될 페이지는 request, response 객체를 공유 

즉, forward 방식은 이동한 URL로 요청정보를 그대로 전달하기에 사용자가 최초로 요청한 요청정보는 다음 URL에서도 유효

 

문법

<jsp:forward page="포워딩될 페이지" />
    <jsp:param name="파라미터 이름" value="파라미터값" />
</jsp:forward>

 

예제  1 (forwardForm1.jsp -> forwardFrom1.jsp -> forwardTo1.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>포워드 실행할 파일  : forwardForm1.jsp</title>
</head>
<body>
	<h3>포워드 첫번째 예제 : 입력받은 값을 forwardFrom1.jsp에게 전달</h3>
	<form action="forwardFrom1.jsp" method="post">
		아이디 : <input type="text" name="id" required="required" />
		<br />
		비밀번호 : <input type="password" name="pwd" required="required" />
		<br />
		<input type="submit" value="로그인" />
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>forwardFrom1.jsp : id와 pwd 파라미터 값을 전달 받음 (request)</title>
</head>
<body>
	<h2>전달 받은 파라미터를 forwardTo1.jsp에게 전달(request)</h2>
	이 페이지는 forwardFrom1.jsp 파일의 내용 입니다
	<br />
	
	<% 
		request.setCharacterEncoding("UTF-8");
		// 아이디를 한글로 입력했을때 깨지는 것을 방지하기 위해 작성 
	%>
	<jsp:forward page="forwardTo1.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>forwardTo1.jsp : forwardFrom1.jsp로부터 전달 받은 request 이용</title>
</head>
<body>
	<%
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
	%>
	
	<%=id %> 님의 비밀번호는 <%= pwd %> 입니다.
</body>
</html>

forwardForm1.jsp 실행 결과
forwardFrom1.jsp 실행 결과

보시다시피 forwardFrom1.jsp에서 바로 forwardTo1.jsp 페이지로 이동 후 실행결과가 클라이언트에게 응답하기 때문에

forwardFrom1.jsp 에서 출력한 내용을 클라이언트는 받지 못한다 

 

 

예제 2 ( 사용자의 이름과 혈액형을 입력받아 각 혈액형별 특징을 보여주는 코드 )

             (forwardForm2.jsp -> forwardFrom2.jsp -> a / b / ab / o.jsp)

forwardForm2.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>forwardForm2.jsp : 이름과  혈액형 선택 값을 forwardFrom2.jsp에게 전달</title>
</head>

<body>
	<form action="forwardFrom2.jsp" method="post">
		이름 : <input type="text" name="name" required="required" />
		<br />
		혈액형은? <br />
		<input type="radio" name="bloodType" value="a" />A형 <br />
		<input type="radio" name="bloodType" value="b" />B형 <br />
		<input type="radio" name="bloodType" value="ab" />AB형 <br />
		<input type="radio" name="bloodType" value="o" />O형 <br />
		
		<input type="submit" value="제출" />
	</form>
</body>
</html>
forwardFrom2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>forwardFrom2.jsp : forwardForm2.jsp로부터 전달받은 파라미터 (request)</title>
</head>

<body>
	<h3>전달받은 파라미터를 이용하여 혈액형.jsp에게 포워딩</h3>
	<%
		request.setCharacterEncoding("UTF-8");
		String bloodTypeFile = request.getParameter("bloodType") + ".jsp";
	%>
	<jsp:forward page="<%=bloodTypeFile %>"></jsp:forward>
	
</body>
</html>
a.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>a.jsp : A형 선택</title>
</head>
<body>
	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>
	<%= name %>님의 혈액형은 <br />
	<%= bloodType %> 형으로 성실하고 신중하며 완벽주의자 입니다.
</body>
</html>

나머지 혈액형 

더보기
b.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>b.jsp : B형 선택</title>
</head>
<body>
	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>
	<%= name %>님의 혈액형은 <br />
	<%= bloodType %> 형으로 규격을 싫어하는 자유인입니다.
</body>
</html>
ab.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ab.jsp : AB형 선택</title>
</head>
<body>
	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>
	<%= name %>님의 혈액형은 <br />
	<%= bloodType %> 형으로 정확한 판단력을 가진 합리주의자입니다.
</body>
</html>
o.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>o.jsp : O형 선택</title>
</head>
<body>
	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>
	<%= name %>님의 혈액형은 <br />
	<%= bloodType %> 형으로 강한 의지의 소유자입니다.
</body>
</html>

 


<jsp:include>

 

서블릿에서 RequestDispacther 객체의 include() 메서드 기능을 실행하는 표준 액션 태그

다른 페이지를 현재 페이지에 포함하고자 할 때 사용하는 태그

해당 페이지를 로딩하다가 include 태그를 만나면 page 속성으로 지정된 페이지에 결과를 해당 위치에 출력한 후 나머지 부분을 로딩함

메인 페이지와 include 된 페이지는 request 를 공유함

 

 

문법

<jsp:include page="삽입할 페이지명" flush="false">
    <jsp:param name="파라미터명" value="파라미터값" />
</jsp:include>

 

 

 

include 지시자와 표준액션 태그의 차이 ?

 

A.jsp 파일에 <%@ include file="B">으로 파일을 포함하면 JSP 파일에 자바 소스로 변환될 때 B페이지가 포함된다

소스가 포함된 후 컴파일되어 클래스 파일이 만들어진다

하지만 A.jsp 파일에 include 액션 태그를 사용하면 실행될 때 B.jsp 파일이 포함된다

따라서 동적으로 포함하려면 include 표준 태그를 사용한다

 

 

 

예시 (includeForm.jsp -> includeMain.jsp -(include)-> includeSub.jsp )

includeForm.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>includeForm.jsp : 입력된 값을 includeMain.jsp에게 전달</title>
</head>
<body>
	<form action="includeMain.jsp">
		이름 : <input type="text" name="name" required="required" />
		<br />
		<input type="submit" value="보내기" />
	</form>
</body>
</html>
includeMain.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>includeForm.jsp 로부터 전달받은 값을 includeSub.jsp에게 전달</title>
</head>

<body>
	<h3>includeMain.jsp은 전달받은 값을 includeSub.jsp에게 전달하고</h3>
	<h3>includeSub.jsp의 결과물을 포함시킨다(돌려받는다)</h3>
	<hr />
	<%
		request.setCharacterEncoding("UTF-8");
	%>
	<jsp:include page="includeSub.jsp" flush="false"></jsp:include>
	
	<hr />
	<h4>includeMain.jsp 파일 내용의 끝부분</h4>

</body>
</html>
includeSub.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String name=request.getParameter("name");
%>
포함되는 페이지 - includeSub.jsp
<br />
<%= name %>님 환영합니다~

includeForm.jsp 실행결과
includeMain.jsp 실행결과

 

예시2 ( includeForm2.jsp -> includeMain2.jsp -(include)-> includeSub2.jsp )

includeForm2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>includeForm2.jsp</title>
</head>

<body>
	<form action="includeMain2.jsp" method="post">
		사이트 주소 : <input type="url" name="siteName" required="required" />
		<br />
		<input type="submit" value="사이트주소 전송" />
	</form>
</body>
</html>
includeMain2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>includeMain2.jsp : includeSub2.jsp 를 포함</title>
</head>
<body>
	<h3>아래 구분선 사이에 포함됩니다</h3>
	<hr />
	<%
		String site = request.getParameter("siteName");
	%>
		<jsp:include page="includeSub2.jsp">
			<jsp:param value="<%=site %>" name="siteURL"/>
		</jsp:include>
	<hr />
</body>
</html>
includeSub2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String site = request.getParameter("siteURL");
%>
includeSub2.jsp 페이지
<br />
<%= site %>
<br />
includeSub2.jsp 페이지 END

includeForm2.jsp 실행 결과
includeMain2.jsp 실행 결과


<jsp:param>

데이터를 넘겨주어 공유하고 싶을 때 사용하는 액션 태그로 <jsp:include>나 <jsp:forward> 액션 태그의 자식 태그로 추가한다

jsp:param 태그는 String 타입만의 값을 전달한다

 

문법

<jsp:forward page="forward 시킬 JSP 페이지">
     <jsp:param name="파라미터키" value="값" />
</jsp:forward>

 

아래와 같이 표현식으로 사용하여 매번 로직에 따라 동적인 값을 넘겨줄 수 있다

<%
     String value = "forwarding value"; //필요에 따라 동적인 값을 할당                
%>
 
 
<jsp:forward page="forwarding.jsp">
     <jsp:param name="파라미터키" value="<%= value %>"/>
</jsp:forward>

forwarding 되는 JSP 쪽에서는 request 객체를 이용해 값을 얻을 수 있다

<%
     String value1 = request.getParameter("파라미터키");                                
%>

 

예시 ( main.jsp -> templateTest.jsp ->  left / bottom / top /  content.jsp)

          ( top.jsp --(<a href>) --> main / comp_forward.jsp --(forward) --> template.jsp )

더보기
main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>main.jsp</title>
</head>

<body>
	<jsp:forward page="templateTest.jsp">
		<jsp:param value="content.jsp" name="CONTENT_PAGE"/>
	</jsp:forward>
</body>
</html>
templateTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>전체 레이아웃 페이지</title>
</head>

<body>
	<% 
		String contentType=request.getParameter("CONTENT_PAGE"); 
	%>
	
	<table border="1" width="600" >
		<tr height="80">
			<td colspan="2" bgcolor="#215f36">
				상단 메뉴 :
				<jsp:include page="top.jsp" flush="false"></jsp:include>
			</td>
		</tr>
		<tr height="500">
			<td valign="top" width="100" bgcolor="#fae100">
				좌측메뉴 : 
				<jsp:include page="left.jsp" flush="false"></jsp:include>
			</td>
			<td valign="top">
				 content.jsp (단 상단메뉴 클릭시 변경 가능)
				 <jsp:include page="<%= contentType %>" flush="false"></jsp:include>
			</td>
		</tr>
		<tr height="80">
			<td colspan="2" bgcolor="#5eba8d">
				botton.jsp
				<jsp:include page="bottom.jsp" flush="false"></jsp:include>
			</td>
		</tr>
	</table>
</body>
</html>
left.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<br />
회사 소개
<br />
제품소개
bottom.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
&nbsp;&nbsp;&nbsp;찾아오시는 길 | 개인보호정책 | 도움말 | 약관 
top.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<a href="main.jsp">HOME</a>&nbsp;&nbsp;
<a href="comp_forward.jsp">회사소개</a>&nbsp;&nbsp;
<a href="#">제품소개</a>
comp_forward.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:forward page="templateTest.jsp">
	<jsp:param value="comp.jsp" name="CONTENT_PAGE"/>
</jsp:forward>
comp.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
여기는 회사 소개 페이지 입니다.. (com.jsp)

main.jsp 실행 결과
회사소개 버튼 눌렀을 때 실행 결과 (top->comp_forward.jsp)