Java Category/JSP

[JSP] 액션 태그(Action tag)

ReBugs 2023. 11. 27.

액션 태그

  • JSP에서 기본으로 제공하는 태그들의 집합으로 서버 또는 클라이언트에게 수행할 명령을 지시
  • 액션 태그를 사용하게 되면 Java 코드를 사용하지 않아도 JSP 웹페이지를 개발할 수 있음
  • 액션 태그는 XML 형식인 <jsp: … />를 사용하며 끝나는 태그는 반드시 />로 마무리해야 함
  • 액션 태그는 JSP 웹페이지를 코딩할 때 Java 코드의 작성을 피하거나 최소화하기 위해 사용
  • JSP 웹페이지에서 Java 코드를 최소화하게 되면 소스 코드에 대한 유지/보수를 효율적으로 수행 가능

 

param

  • 현재 위치한 JSP 웹페이지에서 다른 웹페이지로 정보를 전달할 때 사용하는 태그
  • param 액션 태그는 단독으로 사용할 수 없으므로 <jsp:forward> 태그나 <jsp:include> 태그의 내부에 선언하여 사용
  • param 액션 태그는 여러 개의 파라미터를 선언하여 다른 페이지에 여러 개의 정보를 전달할 수도 있음

 

forward

<jsp:forward page="이동 페이지명"/>
  • response.sendRedirect()와 같이 다른 페이지로 이동(흐름 제어)한다는 점은 같다.
  • 하지만 param을 이용하여 파라미터를 전달할 때 sendRedirect()는 한 번 전달하면 파라미터가 사라지지만
    forward 액션태그는 사라지지 않고 계속 살아있다.

 

 

또한 param태그와 함께 같이 쓰일 수 있다.

<jsp:forward page = "ResponseRedirect.jsp">
        <jsp:param value="mmmm" name="id">
        <jsp:param value="1234" name="phone">
</jsp:forward>

 

예제

더보기

forwardLogin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<center>
	<form action="ResponseProc.jsp" method="post">
	<table width="400">
		<tr height="50">
			<td align="center" width="150"> 아이디 </td>
			<td width="250"> <input type="text" name="id"></td>
		</tr>
		<tr height="50">
			<td align="center" width="150"> 패스워드 </td>
			<td width="250"> <input type="password" name="pass"></td>
		</tr>
		<tr height="50">
			<td align="center" colspan="2">
			<input type="submit" value="로그인">
			&nbsp;&nbsp;
			<input type="reset" value="취소">
		</tr>
	</table>
	</form>
</center>
</body>
</html>

 

ResponseProc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<h2>이 페이지는 로그인 정보가 넘어오는 페이지 입니다.</h2>
	<%
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	//response.sendRedirect("ReponseRedirect.jsp"); //이 페이지로 이동, 당연히 id값을 안넘김
	//response.sendRedirect("ReponseRedirect.jsp?id="+id); //이 페이지로 이동, get방식으로 id넘김, 객체가 살아있는 것이 아님
	%>
	<!-- 객체가 살아 있음, id는 받아온거 넘기고, 번호는 추가적으로 인위적인 것을 넘길 수 있음 -->
	<jsp:forward page="ReponseRedirect.jsp">
		<jsp:param value="01333" name="phone"/>
	</jsp:forward>
<h3>아이디 : <%= id %></h3>
</body>
</html>

 

ReponseRedirect.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<h2>이 페이지는 ReponseRedirect가 넘어오는 페이지 입니다.</h2>
	<%
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String phone = request.getParameter("phone");
	%>
<h3>아이디 : <%= id %></h3>
<h3>아이디 : <%= phone %></h3>
</body>
</html>

 

 

 

include

  • 디렉티브 태그의 include는 스크립틀릿을 사용하는 방식이며 해당 소스를 포함시킨 후 컴파일을 실시한다.
  • 액션 태그의 include는 디렉티브 include와 다르게 실행 시점에 해당 파일을 수행하여 결과를 포함시킨다.

디렉티브 태그의 include는 정적이고, 액션 태그의 include는 동적이고, 독립적이라고 할 수 있다.

 

예제

더보기

 Main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<center>
<%request.setCharacterEncoding("UTF-8");%>
<table width="800" border="1">
	<tr height="150">
		<td align="center" colspan="2">
		<jsp:include page="Top.jsp" >
			<jsp:param value="asd" name="id"/>
		</jsp:include>
		</td>
	</tr>
	<tr height="400">
		<td align="center" width="200"><jsp:include page="Left.jsp"/></td>
		<td align="center" width="600"><jsp:include page="Center.jsp"/></td>
	</tr>
	<tr height="100">
		<td align="center" colspan="2">
		<jsp:include page="Bottom.jsp"/>
		</td>
	</tr>
</table>
</center>
</body>
</html>

 

 Left.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<table width="200">
	<tr height="60">
		<td width=200 align="center"><a href="#"> 스노우 피크 </a></td>
	</tr>
	<tr height="60">
		<td width=200 align="center"><a href="#"> 콜맨 </a></td>
	</tr>
	<tr height="60">
		<td width=200 align="center"><a href="#"> 지프 </a></td>
	</tr>
	<tr height="60">
		<td width=200 align="center"><a href="#"> 코삐아 </a></td>
	</tr>
</table>
</body>
</html>

 

Center.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<table width="600">
	<tr height="400">
		<td align="center">
		<img alt="" src="img/2.jpg" width="400" height="350">
		</td>
	</tr>
</table>
</body>
</html>

 

Bottom.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<table width="800">
	<tr height="100">
		<td align="center">
		이용약관
		이메일무단수집거부
		개인정보처리방침
		영상정보처리기기 운영관리방침
		아이디어 정책
		Copyright. SAMSUNG ELECTRO-MECHANICS All rights reserved.
		삼성전기(주)대표이사 경계현사업자등록번호 124-81-00979
		경기도 수원시 영통구 매영로 150 (매탄동)
		</td>
	</tr>
</table>
</body>
</html>

 

Top.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<%request.setCharacterEncoding("UTF-8");%>
<center>
<table width="800">
	<tr height="100">
		<td colspan="2" align="center" width = 260>
		<img alt ="" src="img/1.png" width="200" height="70">
		</td>
		<td colspan ="4" align="center">
		<font size="10"> 낭만 캠핑 </font>
		</td>
	</tr>
	<tr height="50">
		<td width="110"  align="center"> 텐트 </td>
		<td width="110"  align="center"> 의자 </td>
		<td width="110"  align="center"> 식기류 </td>
		<td width="110"  align="center"> 침낭 </td>
		<td width="110"  align="center"> 테이블 </td>
		<td width="110"  align="center"> 화롯대 </td>
		<td width="140"  align="center"> <%= request.getParameter("id") %> </td>
	</tr>
</table>
</center>
</body>
</html>

 

useBean

자바 빈즈(java Beans)

  • JSP 웹페이지에는 HTML 코드와 스크립트 태그 등을 기술하기 때문에 자칫 프로그램이 복잡하게 작성되어 유지/보수 어려운 상황 초래
  • 자바빈즈 액션 태그를 사용하게 되면 복잡하게 구성된 JSP 웹페이지에서 Java 소스 코드만 따로 뽑아 별도 작성으로 JSP 웹페이지가 복잡해지는 것을 방지할 수 있음
  • 자바빈즈는 데이터 표현을 목적으로 수행하는 Java 클래스로 데이터를 담는 멤버 변수인 프로퍼티(Property)와 데이터를 가져오거나 저장하는 메소드로 구성됨

 

JSP와 자바 빈즈의 프로세스

  1. 웹 브라우저에서 서블릿으로 서비스를 요청
  2. 서블릿은 자바빈즈와 통신을 수행
  3. 자바빈즈는 데이터베이스와 연결하여 데이터를 관리
  4. 서블릿은 JSP 웹페이지에게 정보를 전달
  5.  JSP 웹페이지는 요청한 웹 브라우저에서 전달된 정보를 출력

 

useBean 액션 태그

  • 자바빈즈를 JSP 웹페이지에서 사용하기 위해 Java 클래스를 선언하고 초기화하는 태그로 사용 형식은 다음과 같음

 

useBean 태그 속성

  • id 속성
    id 속성을 사용하여 지정한 객체명의 사용 용도
    -꺼낸 객체의 참조 변수명으로 사용
    -getAttribute( )로 값을 꺼낼 때 사용하는 이름
    -객체를 생성할 경우 보관소에 저장하는 key 값의 이름으로 사용
  • scope 속성
    scope 속성을 사용하여 보관소를 지정
    -page = JspContext(기본값)
    -request = ServletRequest
    -session = HttpSession
    -application = ServletContext
  • class 속성
    -Java 객체를 생성하기 위해 사용할 클래스 이름을 지정할 때 선언
    -new 연산자를 사용하므로 인터페이스는 올 수 없고 반드시 패키지 이름을 포함해야 함
    -scope 속성에서 지정한 보관소에서 객체를 찾지 못하였을 경우 class의 값을 사용하여 인스턴스를 생성
    -생성된 객체는 scope 보관소에 자동으로 저장되며 class 속성을 선언하지 않을 경우 객체를 생성할 수 없게 됨
  • type 속성
    -type 속성은 참조 변수에서 사용할 타입(클래스 또는 인터페이스)을 지정할 때 선언
    -이 속성을 사용할 때는 반드시 패키지 이름을 포함해야 하며 type 속성을 지정하지 않으면 class 속성의 값이 사용됨

 

예제

더보기

memberBean.java(자바 빈즈)

public class MemberBean
{
	private String id;
	private String pass1;
	private String email;
	private String tel;
	private String address;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPass1() {
		return pass1;
	}
	public void setPass1(String pass1) {
		this.pass1 = pass1;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

 

MemberJoin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<center>
	<h2> 회원 가입</h2>
	<form action="MemberJoinProc.jsp" method="post">
		<table width="500" border ="1">
			<tr height = 50>
				<td width="150" align="center"> 아이디 </td>
				<td width="350" align="center"><input type="text" name="id" size="40" placeholder="id를 입력해주세요"></td>
			</tr>
			<tr height = 50>
				<td width="150" align="center"> 패스워드 </td>
				<td width="350" align="center"><input type="password" name="pass1" size="40" 
				placeholder="비밀번호는 영문과 숫자만 넣어주세요"></td>
			</tr>
			<tr height = 50>
				<td width="150" align="center"> 이메일 </td>
				<td width="350" align="center"><input type="email" name="email" size="40"></td>
			</tr>
			<tr height = 50>
				<td width="150" align="center"> 전화번호 </td>
				<td width="350" align="center"><input type="tel" name="tel" size="40"></td>
			</tr>
			<tr height = 50>
				<td width="150" align="center"> 주소 </td>
				<td width="350" align="center"><input type="text" name="address" size="40"></td>
			</tr>
			<tr height = 50>
				<td align="center" colspan="2"><input type="submit" value="회원가입"></td>
			</tr>
		</table>
	</form>
</center>
</body>
</html>

 

MemberJoinProc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<center>
	<h2>회원 정보 보기</h2>
	<%request.setCharacterEncoding("UTF-8");%>
	<jsp:useBean id="mbean" class="bean.MemberBean"> <!-- 객체생성과 유사 -->
		<jsp:setProperty name="mbean" property="*"/> <!-- mbean의 모든 속성을 가져옴 -->
	</jsp:useBean>
	
	<!-- 속성을 출력 -->
	<h2>당신의 아이디는 <jsp:getProperty property="id" name="mbean"/></h2>
	<h2>당신의 패스워드는 <jsp:getProperty property="pass1" name="mbean"/></h2>
	<h2>당신의 이메일은 <jsp:getProperty property="email" name="mbean"/></h2>
	<h2>당신의 전화번호는 <jsp:getProperty property="tel" name="mbean"/></h2>
	<h2>당신의 주소는 <%= mbean.getAddress() %></h2>
</center>
</body>
</html>

 

 

 

'Java Category > JSP' 카테고리의 다른 글

[JSP] 파일 업로드  (0) 2023.12.25
[JSP] JSP와 데이터베이스 연동  (2) 2023.11.27
[JSP] 세션(Session)  (1) 2023.11.25
[JSP] 쿠키(Cookie)  (2) 2023.11.24
[JSP] 내장 객체  (2) 2023.11.24

댓글