[JSP] 15. JSTL(JSP Standard Tag Library) #2 코어 태그(import, url, redirect, out, catch)
2022. 6. 29. 13:28ㆍJAVA/Servlet&JSP
1. URL 처리 태그
- import 태그
- redirect 태그
- url 태그
1.1 import 태그
import 태그는 <jsp:include> 태그와 비슷한 기능을 제공하는 태그로서 사용 방법은 다음과 같습니다.
<c:import url="http://media.daum.net/"
charEncoding="euc-kr"
var="daumNews"
scope="request">
<c:param name="_top_G" value="news"/>
</c:import>
- url : 읽어 올 URL 주소
- charEncoding : 읽어 온 데이터의 문자집합
- var : 읽어 온 데이터를 저장할 변수명
- scope : 변수를 저장할 범위 지정
param 태그는 <jsp:param> 태그처럼 전송할 파라미터의 이름과 값을 지정할 때 사용됩니다.
<jsp:include> 태그와 <c:import> 태그의 차이점
- <jsp:include> 태그는 같은 웹 어플리케이션 내의 자원만을 포함할 수 있습니다.
- <c:import> 태그는 같은 웹 어플리케이션 내의 자원뿐만 아니라 다른 웹 어플리케이션 또는 다른 서버의 자원까지도 접근할 수 있습니다.
import 태그에서 url 속성의 값 3가지 형태
- 완전한 URL : http://localhost:8080/jstl/importTag.jsp
- 웹 어플리케이션 내에서의 절대 경로 : /url.jsp
- 웹 어플리케이션 내에서의 상대 경로 : ./data/a.txt
다음은 import 태그의 사용 예제입니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:import url="/Chapter15/url.jsp" var="urlEx"/>
<c:import url="http://localhost:8080/jsp2/Chapter15/header.jsp" var="head">
<c:param name="id" value="user1"/>
</c:import>
<c:import url="./footer.jsp" var="foot"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>import 태그</title>
</head>
<body>
${head}
${urlEx}
${foot}
</body>
</html>
1.2 url 태그
url 태그는 URL을 생성해서 변수에 저장할 때 사용되며, 사용 방법은 다음과 같습니다.
<c:url var="url1" value="/shopping.do" scope="session">
<c:param name="Add" value="isdn-001"/>
</c:url>
- var : 생성한 URL이 저장될 변수명
- value : 생성할 URL
- scope : 변수를 저장할 범위 지정
value 속성값 종류
- 완전한 URL : http://localhost:8080/jstl/importTag.jsp
- 웹 어플리케이션 내에서의 절대 경로 : /url.jsp
- 현재 경로에 대한 상대 경로 : ./data/a.txt
다음은 url 태그를 사용한 예제입니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:url var="url1" value="../shopping.do"/>
<c:url var="url2" value="/shopping.do">
<c:param name="Add" value="isbn-001"/>
</c:url>
<c:url var="url3" value="http://localhost:8080/jsp2/setTag.jsp"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>url 태그</title>
</head>
<body>
<ul>
<li>URL1 = ${url1}</li>
<li>URL2 = ${url2}</li>
<li>URL3 = ${url3}</li>
</ul>
</body>
</html>
1.3 redirect 태그
redirect 태그는 지정한 페이지로 리다이렉트할때 사용됩니다. response.sendRedirect()와 비슷한 기능을 제공합니다.
<c:redirect url="/ifTag.jsp">
<c:param name="name" value="김용환"/>
</c:redirect>
2. 기타 태그
- out 태그 : 지정한 값을 JspWriter에 출력
- catch 태그 : 에러 메시지를 지정한 변수에 저장
2.1 out 태그
out 태그는 JspWriter에 데이터를 출력할 때 사용되는 태그로서 다음과 같이 사용할 수 있습니다.
<c:out value="value" escapeXml="{true|false}" default="defaultValue"/>
out 태그의 속성
- value : JspWriter에 출력할 값을 나타냅니다. 일반적으로 value 속성의 값은 String과 같은 문자열입니다. 만약 value의 값이 java.io.Reader의 한 종류라면 out 태그는 Reader로부터 데이터를 읽어 JspWriter에 값을 출력합니다.
- escapeXml : 이 속성의 값이 true일 경우 아래 표와 같이 문자를 변경합니다. 생략할 수 있으며, 생략할 경우 기본값은 true입니다.
- default : value 속성에서 지정한 값이 존재하지 않을 때 사용될 값을 지정합니다.
escapeXml 속성이 true일 경우 반환되는 문자
문자 | 변환된 형태 |
< | < |
> | > |
& | & |
' | ' |
" | " |
다음 예제는 path 경로에서 지정한 파일의 내용을 화면에 보여주는 예제입니다.
<%@page import="java.io.IOException"%>
<%@page import="java.io.FileReader"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>out 태그</title>
</head>
<body>
<%
String path = request.getParameter("path");
try(FileReader reader = new FileReader(getServletContext().getRealPath(path))){
%>
<pre>
소스코드 = <%=path %>
<c:out value="<%=reader %>" escapeXml="true"/>
</pre>
<%
}catch(IOException e){
%>
에러 : <%= e.getMessage()%>
<%
}
%>
</body>
</html>
2.2 catch 태그
catch 태그는 예외가 발생할때 사용되는 태그입니다.
<c:catch var="exName">
...
예외가 발생할 수 있는 코드
...
</c:catch>
catch 태그 블록에서 예외가 발생할 경우 그 예외 객체는 exName 변수에 할당됩니다.
다음은 catch 태그를 사용한 예제입니다.
<%@page import="java.io.IOException"%>
<%@page import="java.io.FileReader"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>catch 태그</title>
</head>
<body>
<c:catch var="result">
name 파라미터의 값 = <%=request.getParameter("name") %><br>
<%
if(request.getParameter("name").equals("test")){
%>
${param.name}은 test입니다.
<%
}
%>
</c:catch>
<p>
<c:if test="${result != null}">
예외가 발생했습니다.<br>
</c:if>
</p>
</body>
</html>
References
source code : https://github.com/yonghwankim-dev/JSP2.0
JSP 2.0 프로그래밍 기초부터 중급까지
'JAVA > Servlet&JSP' 카테고리의 다른 글
[JSP] 15. JSTL(JSP Standard Tag Library) #2 코어 태그(set, remove, if, choose, forEach, forTokens) (0) | 2022.06.29 |
---|---|
[JSP] 15. JSTL(JSP Standard Tag Library) #1 JSTL이란 무엇인가? (0) | 2022.06.29 |
[Java][Servlet] Servlet & JSP의 MVC 패턴 (0) | 2022.05.23 |
[Tomcat] Tomcat, JSP, Servlet 개념 및 구조 (0) | 2022.05.23 |
[JSP] 14. 표현 언어(Expression Language) (0) | 2022.04.28 |