CH04 액션 태그1

Gm·2021년 9월 13일
0

JSP 수업

목록 보기
2/10

액션 태그(Action tag)

  • 서버나 클라이언트에게 어떤 행동을 하도록 명령하는 태그
  • JSP 페이지에서 페이지와 페이지 사이 제어
  • 다른 페이지의 실행 결과 내용을 현재 페이지에 포함
  • 자바 빈즈(JavaBeans) 등 다양한 기능 제공
  • XML 형식 사용

    ※ XML(extend markup language)
    - html 확장기능
    - eml : 이메일 확장자


    -> 환경설정에서 더 많이 사용함
    -> html에 비교했을 때 문법적으로 엄격하고 기능이 많다
    -> Andoroid 할 때 xml로 페이지 디자인


* 액션 태그의 종류



4-1 include Tag

  • 현재 jsp 페이지에서 다른 페이지로 이동하는 태그

  • ex01) <jsp:forward page=" "/>

<forwardTag1.html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"/>
</head>
<body>
<h1>Forward Tag Example1</h1>
<FORM METHOD=POST ACTION="forwardTag1_1.jsp">
아이디 : <INPUT  NAME="id" value="aaa"><p>
패스워드 : <INPUT TYPE="password" NAME="pwd" value="1234"><p>
<INPUT TYPE="submit" VALUE="보내기">
</FORM>
</body>
</html>

==> 결과

<forwardTag1_1.jsp>

<%@ page contentType="text/html; charset=EUC-KR"%>
<%
	request.setCharacterEncoding("EUC-KR");	
%>
현재의 페이지는 동적인 값으로 인한 다른 페이지 전달(경유)의 목적으로 존재
현재 페이지의 텍스트는 보이지 않음.
forwardTag1.html에서 요청된 id, pwd도 같이 전달된다.
<jsp:forward page="forwardTag1_2.jsp"/>	

<forwardTag1_2.jsp>

<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");
	String id = request.getParameter("id");
	String pwd = request.getParameter("pwd");
%>
id : <%=id %>
pwd : <%=pwd %>

==>결과


ex02)

<forwardTag2.html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"/>
</head>
<body>
<h1>Forward Tag Example2</h1>
<FORM METHOD="get" ACTION="forwardTag2_1.jsp">
혈액형별로 성격 테스트<p/>
당신의 혈액형은?<p/>
<INPUT TYPE="radio" NAME="bloodType" VALUE="A">A형<br>
<INPUT TYPE="radio" NAME="bloodType" VALUE="B">B형<br>
<INPUT TYPE="radio" NAME="bloodType" VALUE="O">O형<br>
<INPUT TYPE="radio" NAME="bloodType" VALUE="AB">AB형<br>
<INPUT TYPE="submit" VALUE="보내기">
</FORM>
</body>
</html>

<forwardTag2_1.jsp>

<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");
String name = "포길동";
String bloodType = request.getParameter("bloodType");
%>
<jsp:forward page='<%=bloodType+".jsp" %>'>
<jsp:param value="<%=name %>" name="name"/>
</jsp:forward>

<A.jsp>

<h1>Forward Tag Example2</h1>
<%@ page contentType="text/html;charset=EUC-KR"%>
<%
 String name = request.getParameter("name");
 String bloodType = request.getParameter("bloodType");
%>
<b><%=name%></b>님의 혈액형은
<b><%=bloodType%></b>형이고
성실하고 신중하며 완벽주의자입니다.

<B.jsp>

<h1>Forward Tag Example2</h1>
<%@ page contentType="text/html;charset=EUC-KR"%>
<%
 String name = request.getParameter("name");
 String bloodType = request.getParameter("bloodType");
%>
<b><%=name%></b>님의 혈액형은
<b><%=bloodType%></b>형이고 
규격을 싫어하는 자유인입니다.

<O.jsp>

<h1>Forward Tag Example2</h1>
<%@ page contentType="text/html;charset=EUC-KR"%>
<%
 String name = request.getParameter("name");
 String bloodType = request.getParameter("bloodType");
%>
<b><%=name%></b>님의 혈액형은
<b><%=bloodType%></b>형이고
강한 의지의 소유자입니다.

<AB.jsp>

<h1>Forward Tag Example2</h1>
<%@page contentType="text/html;charset=EUC-KR"%>
<%
	String name = request.getParameter("name");
	String bloodType = request.getParameter("bloodType");
%>
<%=name%>님의 혈액형은
<b><%=bloodType%></b>형이고<p>
정확한 판단력을 가진 합리주의자입니다.

==> 결과



4-2 include Tag

  • 현재 jsp 페이지의 특정 영역에 외부 파일의 내용을 포함하는 태그
    (include 디렉티브 태그<@include >의 경우 주로 정적 페이지 사용시 이용됨)
  • 현재 JSP 페이지에 포함할 수 있는 외부 파일은 HTML, JSP, 서블릿 페이지 등

ex01)

<includeTag1.html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"/>
</head>
<body>
<h1>Include Tag Example1</h1>
<FORM METHOD="POST" ACTION="includeTag1.jsp">
이름 : <INPUT NAME="name" value="aaa"><p/>
<INPUT TYPE="submit" VALUE="보내기">
</FORM>
</body>
</html>

<includeTag1.jsp>

<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");%>
<!-- include 액션 태그는 현재페이지에 요청된 정보까지 같이 넘겨줌 -->
<!-- include 액션태그 요청되는 정보에 따라 동적인 페이지가 포함 -->
<jsp:include page="includeTagTop1.jsp" />

<includeTagTop1.jsp>

<!-- includeTagTop1.jsp -->
<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");
	String name = request.getParameter("name");
%>
include Action Tag의 Top입니다.<p>
<b><%=name%></b>

==> 결과


ex02)

<includeTag2.html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"/>
</head>
<body>
<h1>Include Tag Example2</h1>
<FORM METHOD="POST" ACTION="includeTag2.jsp">
SITENAME : <INPUT NAME="siteName" value="naver.com"><p>
<INPUT TYPE ="submit" VALUE="보내기">
</FORM>
</body>
</html>  

<includeTag2.jsp>

<!-- includeTag2.jsp -->
<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");
	String siteName = request.getParameter("siteName");
%>
사이트명 : <%=siteName%> <br>
<jsp:include page="includeTagTop2.jsp">
	<jsp:param value="aaa" name="id"/>
	<jsp:param value="1234" name="pwd"/>	
</jsp:include>

<includeTagTop2.jsp>

<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");
	String id = request.getParameter("id");
	String pwd = request.getParameter("pwd");
%>
<br>
id : <%=id%> / pwd : <%=pwd%>

==> 결과


ex03)

<includeTag3.html>

<!-- includeTag3.html -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"/>
</head>
<body>
<h1>Include Tag Example3</h1>
<FORM METHOD="get" ACTION="includeTag3.jsp">
혈액형별로 성격 테스트<p/>
당신의 혈액형은?<p/>
<INPUT TYPE="radio" NAME="bloodType" VALUE="A">A형<br>
<INPUT TYPE="radio" NAME="bloodType" VALUE="B">B형<br>
<INPUT TYPE="radio" NAME="bloodType" VALUE="O">O형<br>
<INPUT TYPE="radio" NAME="bloodType" VALUE="AB">AB형<br>
<INPUT TYPE="submit" VALUE="보내기">
</FORM>
</body>
</html>

<includeTag3.jsp>

<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");
	String name = "홍길동";
	String bloodType = request.getParameter("bloodType");
%>
<!-- 표현식에서 ""안에 값이 있을 때는 ''값으로 시작 -->
<jsp:include page='<%=bloodType + ".jsp" %>'>
	<jsp:param value="<%=name %>" name="name"/>
</jsp:include>

<A.jsp>

<h1>Forward Tag Example2</h1>
<%@ page contentType="text/html;charset=EUC-KR"%>
<%
 String name = request.getParameter("name");
 String bloodType = request.getParameter("bloodType");
%>
<b><%=name%></b>님의 혈액형은
<b><%=bloodType%></b>형이고
성실하고 신중하며 완벽주의자입니다.

<B.jsp>

<h1>Forward Tag Example2</h1>
<%@ page contentType="text/html;charset=EUC-KR"%>
<%
 String name = request.getParameter("name");
 String bloodType = request.getParameter("bloodType");
%>
<b><%=name%></b>님의 혈액형은
<b><%=bloodType%></b>형이고 
규격을 싫어하는 자유인입니다.

<O.jsp>

<h1>Forward Tag Example2</h1>
<%@ page contentType="text/html;charset=EUC-KR"%>
<%
 String name = request.getParameter("name");
 String bloodType = request.getParameter("bloodType");
%>
<b><%=name%></b>님의 혈액형은
<b><%=bloodType%></b>형이고
강한 의지의 소유자입니다.

<AB.jsp>

<h1>Forward Tag Example2</h1>
<%@page contentType="text/html;charset=EUC-KR"%>
<%
	String name = request.getParameter("name");
	String bloodType = request.getParameter("bloodType");
%>
<%=name%>님의 혈액형은
<b><%=bloodType%></b>형이고<p>
정확한 판단력을 가진 합리주의자입니다.



==> 결과



4-3 useBean

  • jsp 페이지에서 자바빈즈를 사용하기 위해 실제 자바 클래스를 선언하고 초기화하는 태그
  • id 속성과 scope 속성을 바탕으로 자바빈즈의 객체를 검색하고, 객체가 발견되지 않으면
    빈 객체를 생성




1) setProperty 액션 태그

  • useBean 액션 태그와 함께 자바빈즈의 setter() 메서드에 접근하여 자바빈즈의 멤버
    변수인 프로퍼티의 값을 저장하는 태그

2) getProperty 액션 태그

  • useBean 액션 태그와 함께 자바빈즈의 getter() 메서드에 접근하여 자바빈즈의 멤버 변수인 프로퍼티의 값을 가져오는 태그

ex01)

<simpleBean.html>

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">
	function send() {		
		f=document.frm;
		//f=document.forms[0];
		f.action="simpleBean2.jsp";
		f.submit();
	}
</script>
</head>
<body>
<form name="frm" action="simpleBean.jsp">
msg : <input name="msg" value="빈을 만들자."><br/>
cnt : <input name="cnt" value="10"><br/>
<input type="submit" value="Send1">
<!-- button type은 반드시 onclick 실행 -->
<input type="button" value="Send2" onclick="send()">
</form>
</body>
</html>

<SimpleBean2.jsp>

<!-- LGH 2021SEP14 --> 
<!-- 자바빈즈(JavaBeans) -->
<!-- SimpleBean2.jsp -->
<%@page import="ch04.SimpleBean" %>
<%@page contentType="text/html; charset=EUC-KR"%>
<% 
		request.setCharacterEncoding("EUC-KR");
%>
<!-- SimpleBean bean = new SimpleBean() 과 같은 기능 -->
<jsp:useBean id="bean" class="ch04.SimpleBean"/>
<%-- <jsp:setProperty property="msg" name="bean"/>
<jsp:setProperty property="cnt" name="bean"/> --%>
<jsp:setProperty property="*" name="bean"/>
msg: <jsp:getProperty property="msg" name="bean"/> <br>
cnt: <jsp:getProperty property="cnt" name="bean"/> <br>
msg2: <%=bean.getMsg() %> <br>
cnt2: <%=bean.getCnt()%> <br>

<SimpleBean.java>

// LGH-2021SEP14
// SimpleBean.java
package ch04;
/* 자바빈즈(JavaBeans) 네이밍
* 1. 테이블명+Bean (TeamBean) - JSP 사용
* 2. 테이블+Dto (Data Transfer Object) - Spring 사용
* 3. 테이블+Vo (Value Object) - Spring 사용
**/
public class SimpleBean {
	private String msg;
	private int cnt;	
	//getter -> getXxx
	public String getMsg() {
		return msg;
	}
	//setter -> setXxx
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getCnt() {
		return cnt;
	}
	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
}

useBean - scope 속성 예제

: scope의 속성 값 - page, session

  • 세션(session)
    • 톰켓 서버에 Client 단위로 객체가 만들어짐(영속성 아님)
    • 세션은 쿠키에 저장됨
  • 쿠키
    • 브라우저가 클라우드(pc)에서 기억하는 저장 위치(값), 본인 pc에 파일로 저장(영속성)


      <scopeBean.jsp>
<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");%>
<%
	//session.setMaxInactiveInterval(10); // 세션 유지 시간 : 10초
%>
<jsp:useBean id="pBean" class="ch04.ScopeBean" scope="page"/>
<jsp:useBean id="sBean" class="ch04.ScopeBean" scope="session"/>
<jsp:setProperty property="num" name="pBean" value="<%=pBean.getNum()+10 %>"/>
<jsp:setProperty property="num" name="sBean" value="<%=sBean.getNum()+10 %>"/>
<h2>Scope Bean</h2>
pBean : <jsp:getProperty property="num" name="pBean"/>
sBean : <jsp:getProperty property="num" name="sBean"/>
세션 id : <%=session.getId()%> <br><br><br>
<a href="scopeBean2.jsp">세션제거</a>

<ScopeBean.java>

package ch04;
public class ScopeBean {
	private int num;	
	public int getNum() {
		return num;
	}	
	public void setNum(int num) {
		this.num = num;
	}	
}

<scopeBean2.jsp>

<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");
	session.invalidate(); // 세션 제거 메서드(무효화)
	response.sendRedirect("scopeBean.jsp");
%>

0개의 댓글

Powered by GraphCDN, the GraphQL CDN