Servlet & JSP – 28 – JSP Standard Tag Library – 03

Merhaba Arkadaslar,
Bu bolumde JSP Standard Tag Library (JSTL) konusuna devam edecegiz.
Bu bolumde isleyecegimiz JSTL core taglari ;

  • <c:import>
  • <c:param>
  • <c:url>
  • <c:redirect>
  • <c:catch>

<c:import> & <c:param>

Daha oncesinde include directive’i ve <jsp:include> Standard Action yapisini kullandik. Static Inclusion & Dynamic Inclusion konusunda bunlari incelemistik.

<c:import> JSTL etiketi de inclusion amaciyla kullanilmaktadir ve dynamic inclusion ozelligine sahiptir.

<jsp:include> ve include directive ile mevcut projedeki sayfalari projemize ekleyebiliriz.
<c:import> jstl etiketi ile proje disindan herhangi bir kaynak import edilebilir.

includeDirective.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<%@include file="header.html" %>
	
	<p> this is includeDirective.jsp </p>
	
	<%@include file="footer.html" %>
	
</body>
</html>

includePage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<jsp:include page="header.html"/>
	
	<p> this is includePage.jsp </p>
	
	<jsp:include page="footer.html"/>
	
</body>
</html>

cimport.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<c:import url="header.html"/>
	
	<p> this is cimport.jsp </p>
	
	<c:import url="footer.html"/>
	
</body>
</html>

include directive file , <jsp:include> Standard Action’i page , <c:import> JSTL etiketi ise url attribute’u alir.

<jsp:include> action’i icerisinde <jsp:param> etiketinin kullanimi incelemistik. include ettigimiz sayfa icin parametre verebiliyoruz.

paramInclude.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<body>

	<jsp:include page="param.jsp">
		<jsp:param name="title" value="title test" />
	</jsp:include>

</body>
</html>

param.jsp

<strong>${param.title }</strong>

Benzer sekilde <c:import> JSTL etiketi icerisinde <c:param> JSTL etiketini kullanabiliriz.

cimport2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<c:import url="param.jsp">
		<c:param name="title" value="title test"></c:param>
	</c:import>
	
</body>
</html>

<c:url> & <c:param>

Session bolumunde URL rewriting konusunu incelemistik. Eger browser cookie’si kapaliysa bu durumda URL rewriting otomatik olarak calisacaktir. Bu durumda JSESSIONID , URL linkine eklenecektir.

curl.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<%
		String encodedUrl = response.encodeURL("encodeMe.jsp");
	%>

	<a href="<%=encodedUrl%>">Click Me!</a>

	<%-- 
		
	<c:url> otomatik olarak URL rewriting islemi yapacaktir.
	Eger cookie kapali olursa JSESSIONID url sonuna eklenecektir. -->
	
	--%>

	<a href='<c:url value="encodeMe.jsp" />'>Click here</a>

	<%--Eger encodeURL metodu ya da <c:url> JSTL etiketi kullanilmazsa 
	cookie kapaliysa bu durumda JSESSIONID url sonuna eklenmez.. --%>
	
	<a href='encodeMe.jsp'>Click here</a>

</body>
</html>

<c:url> JSTL etiketi icerisinde <c:param> JSTL etiketini kullanabiliriz.

curl2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

	<c:set var="firstName" value="Levent" />	
	<c:set var="lastName" value="Erguder" />

	<a href='<c:url value="encodeMe2.jsp?name=${firstName}&surname=${lastName}" />'>Click here</a>
	
	<a href='
	<c:url value="encodeMe2.jsp"> 
		<c:param name="firstName" value="Levent"></c:param>
		<c:param name="lastName" value="Erguder"></c:param>
	</c:url> '> Click here</a>

	
</body>
</html>

<c:redirect>

<c:redirect> JSTL etiketi sendRedirect metoduna benzer sekilde calisir.

credirect.jsp

	<%
	//	response.sendRedirect("redirectMe.jsp");
	%>

	<c:redirect url="redirectMe.jsp"/>

<c:catch>

<c:catch> JSTL try-catch blogu gibi calisir fakat <c:try> diye bir JSTL etiketi yoktur. Ornegimizi inceleyelim ;

ccatch.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

<c:catch var ="catchException">
   <% int x = 10/0;%>
</c:catch>

<c:if test = "${catchException != null}">
   <p>The exception is : ${catchException} <br />
   There is an exception: ${catchException.message}</p>
</c:if>

	
</body>
</html>

10/0 islemi ArithmeticException’a neden olur. <c:catch> JSTL etiketi icerisinde yapildigi icin bu hata yakalanir. Browserda gosterilmez.

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *