Servlet & JSP – 25 – Static Inclusion & Dynamic Inclusion

Merhaba Arkdaslar,
Bu bolumde static inclusion ve dynamic inclusion konularini inceleyecegiz’

  • include directive
  • <jsp:include> 
  • <jsp:forward>

static inclusion

static inclusion da bir baska dosyanin icerigi mevcut JSP dosyasina translation zamaninda dahil edilir(include) ve translate sonrasinda bir tane servlet dosyasi(.java) olusur.

static inclusion; include directive kullanilarak yapilir.

Genel format ;

<%@ include file="relativeURL" %>

include directive’i sadece file attribute’u alir. file HTML , XML , txt ya da JSP dosyasi olabilir. Servlet dosyasi olamaz.

header.html

<p>header.html dosyasi baslangici</p>
<h1>header1</h1>
<h2>header2</h2>
<p>header.html dosyasi sonu</p>

includeMe.jsp

 <h1>included.jsp file</h1>

footer.txt

this is footer file content.

staticInclude.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<body>
	<p>include.jsp dosyamiza header.html dosyasinin icerigini ekledik.</p>

	<%@include file="header.html"%>

	<p>includeMe.jsp dosyasini ekleyelim;</p>

	<%@include file="includeMe.jsp"%>

	<p>footer.txt dosyasini ekleyelim;</p>

	<%@include file="footer.txt"%>

</body>
</html>

include directive’in calisma mantigi gayet kolaydir. header.html , includeMe.jsp , footer.txt dosyalarini include.jsp dosyamizin icerisine aktardik.

Burada dikkat etmemiz gereken nokta header.html dosyamizin icerisinde <html> <body> gibi etiketler olmamali, cunku zaten jsp dosyamizda <html> <body> etiketlerimiz mevcut.

    • file attribute u expression olamaz. Ornegin su kod gecersizdir , derleme hatasi verir.
<% String myURL ="copyright.html"; %>
<%@ include file="<%= myURL %>" %>
    • file attribute’u included edilen sayfaya parametre gecemez. Yani su kod gecersiz olacaktir ;
<%@ include file="other.jsp?name=Levent" %>

dynamic inclusion

Dynamic inclusion’da , JSP saygasina istekte bulunuldugunda , bu JSP sayfasi include edilmesi gereken dosyalar icin calisma zamaninda bir istekte daha bulunur.

dynamic inclusion olarak <jsp:include> ve<jsp:forward> standard action’lar kullanilir.

<jsp:include page="relativeURL" flush="true" />
<jsp:forward page="relativeURL" />

dynamic inclusion icin su uc yontemi de kullanabiliriz ;

dynamicInclude.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<body>
	
	<!-- Bu uc yontem de ayni isi yapar! dynamic inclusion -->
	
	<!-- 
	    request JSP implicit objesini kullanarak RequestDispatcher objesi elde edebiliriz.
	    include metodunu kullanarak dynamic inclusion islemini yapabiliriz
	 -->
	 
	<p>##### rd.include ##### </p>
	 
	<%
		RequestDispatcher rd = request.getRequestDispatcher("includeMe.jsp");
		rd.include(request, response);
	%>

        <p>##### pageContext.include ##### </p>
     
	<%
		pageContext.include("includeMe.jsp");
	%>

	<p>##### jsp include action ##### </p>
	
	<jsp:include page="includeMe.jsp"  />

</body>
</html>
  • include directive translation zamaninda calisir , <jsp:include> runtime da calisir.
  • include file attribute alir , jsp include page attribute alir.
  • static inclusion da bir tane servlet dosyasi olusur. dynamic inclusion da her jsp icin bir servlet dosyasi olusur.
  • Hem static inclusion’ hem dynamic inclusion kullanarak HTML ,XML , txt gibi dosyalari include islemi yapabiliriz.
  • Bir JSP Sayfasinda dynamic inclusion ile servlet’i include edebiliriz.
  • static inclusion da  bu mumkun degildir.

Bir JSP dosyasi Servlet dosyasini dynamic olarak include edebilir ;

DynamicIncludeMe.java

package _12_DynamicInclude;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DynamicIncludeMe extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		PrintWriter pw = resp.getWriter();

		pw.println("DynamicIncludeMe servlet!");

	}
}

	<servlet>
		<servlet-name>DynamicIncludeMe</servlet-name>
		<servlet-class>_12_DynamicInclude.DynamicIncludeMe</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>DynamicIncludeMe</servlet-name>
		<url-pattern>/dynamicIncludeMe</url-pattern>
	</servlet-mapping>

dynamicServletInclude.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<body>
	
	<!-- Bu uc yontem de ayni isi yapar! dynamic inclusion 
	Bir JSP Sayfasinda dynamic inclusion ile servlet'i include edebiliriz.
	-->
		 
	<p>##### rd.include ##### </p>
	 
	<%
		RequestDispatcher rd = request.getRequestDispatcher("/dynamicIncludeMe");
		rd.include(request, response);
	%>

       <p>##### pageContext.include ##### </p>
    
	<%
		pageContext.include("/dynamicIncludeMe");
	%>

	<p>##### jsp include action ##### </p>
	
	<jsp:include page="/dynamicIncludeMe"  />

</body>
</html>
  • page attribute’u expression olabilir ! include directive’i icin file attribute expression olamaz.

validexpression.jsp

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


	<%
		String pageURL = "includeMe.jsp";
	%>
	
	<jsp:include page="<%=pageURL%>" />

</body>
</html>

<jsp:param>

<jsp:include> action’i body/govde alabilir. Dynamic inclusion da include edilen sayfaya parametre gecilebilir.  Bunun icin <jsp:param> kullanilabilir.

Yeni bir jsp dosyasi olusturalim ;

param.jsp

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

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>

<jsp:forward>

Bir JSP dosyamizdan diger JSP ya da Servlet dosyasina yonlendirme yapilabilir(forward/ request dispatch)

Bu islem 3 yontemle yapilabilir;

Yontem1 ;

forwardIt.jsp

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

<html>
<body>

	<p>forwardIt.jsp , bu kismi goremeden handleIt.jsp dosyasina forward edilecek. </p>
	<jsp:forward page="handleIt.jsp"></jsp:forward>

</body>
</html>

handleIt.jsp

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

<html>
<body>
        <h1>forwarded by dispatchMe.jsp</h1>
        <h1>This is handleIt.jsp</h1>
</body>
</html>

Dikkat edecek olursak forwardIt.jsp dosyamizdan , handletIt.jsp dosyamiza forward etttik fakat url forwardIt.jsp  olarak kaldi.

forward islemi yapmadan once out.print out.flush gibi metotlar kullanilirsa  ;

java.io.IOException: Error: Attempt to clear a buffer that’s already been flushed

Yontem 2 ;

forwardIt2.jsp

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

<html>
<body>

	<%
		pageContext.forward("handleIt2.jsp");
	%>

</body>
</html>

Yontem 3 :

dispatchMe.jsp

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

<html>
<body>

	<%
		RequestDispatcher rd = request.getRequestDispatcher("handleIt3.jsp");
		rd.forward(request, response);
	%>

</body>
</html>

handleIt3.jsp

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

<html>
<body>

	<h1>Dispatched by dispatchMe.jsp</h1>
	<h1>This is handleIt3.jsp</h1>

</body>
</html>

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 *